2

I have follow this documnet https://devdocs.magento.com/guides/v2.3/extension-dev-guide/declarative-schema/db-schema.html but not get success

module.xml

        <?xml version="1.0"?>
        <!--
        /**
         * Copyright © Krish TechnoLabs, All rights reserved.
         */
        -->
        <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
            <module name="Ktpl_Test1" setup_version="1.0.0">
                <sequence>
                    <module name="Magento_Eav"/>
                </sequence>
            </module>
        </config>

db_schema.xml

                <?xml version="1.0"?>
        <!--
        /*
         * Copyright © 2019 Krish Technolabs. All rights reserved.
         * See COPYING.txt for license details
         */
        -->
        <schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
            <table comment="Test Table 1" engine="innodb" name="ktpl_test1" resource="default">
                <column identity="true" name="test1_id" nullable="false" padding="10" unsigned="true" xsi:type="int"/>
                <column  name="test_name" nullable="false" padding="10" unsigned="true" xsi:type="int"/>
                <column   name="test2_id" nullable="false" padding="10" unsigned="true" xsi:type="int"/>
                <constraint referenceId="PRIMARY" xsi:type="primary">
                    <column name="test1_id"/>
                </constraint>
               <constraint xsi:type="foreign" referenceId="Test12ID" table="ktpl_test1" column="test2_id" referenceTable ="ktpl_test2" referenceColumn="test2_id"/>
               <constraint xsi:type="foreign" referenceId="Test13ID" table="ktpl_test1" column="test3_id" referenceTable ="ktpl_test3" referenceColumn="test3_id"/>
            </table>
             <table name="eav_attribute" resource="default" engine="innodb" comment="Eav Attribute">
                <column xsi:type="mediumtext" name="note" nullable="false" length="500" comment="Note"/>
                <constraint xsi:type="primary" referenceId="PRIMARY">
                    <column name="attribute_id"/>
                </constraint>
            </table>
        </schema>
2

The only mistake you are doing here is length attribute, just remove it.

This is the working code..

etc\db_schema.xml

<?xml version="1.0"?>

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="eav_attribute" resource="default" engine="innodb" comment="Eav Attribute">
        <column xsi:type="mediumtext" name="note" nullable="true" comment="Note"/>
    </table>
</schema>
1
  • getting Invalid Document Element 'column', attribute 'length': The attribute 'length' is not allowed. Line: 297 error – Rutvee Sojitra Jul 24 '19 at 5:57
0

Create db_schema.xml file in your module and add this below code :

app/code/VendorName/ModuleName/etc/db_schema.xml

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="eav_attribute">
        <column xsi:type="mediumtext" name="note" nullable="true" length="255" comment="Note"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="attribute_id"/>
        </constraint>
    </table>
</schema>

Now, execute this below command in your Magento root directory

php bin/magento setup:db-declaration:generate-whitelist --module-name=VendorName_ModuleName

Make sure, there are db_whitelist_schema.json file will be create in app/code/VendorName/ModuleName/etc folder.

Now, upgrade, deploy and clean cache

php bin/magento s:up
php bin/magento s:s:d -f
php bin/magento c:c

enter image description here

Hope, it will helpful for you.


UPDATE :

de_whitelist_schema.json (After install ktpl_test1 table) :

{
    "ktpl_test1": {
        "column": {
            "test1_id": true,
            "test_name": true,
            "test2_id": true
        },
        "constraint": {
            "PRIMARY": true
        }
    },
    "eav_attribute": {
        "column": {
            "note": true
        },
        "constraint": {
            "PRIMARY": true
        }
    }
}
10
  • getting error when updating doing s:up The XML in file "/var/www/html/Magento231new/app/code/Ktpl/Test1/etc/db_schema.xml" is invalid: Element 'column', attribute 'length': The attribute 'length' is not allowed. Line: 20 – Rutvee Sojitra Jul 24 '19 at 5:35
  • Did you generate whitelist file? – Rohan Hapani Jul 24 '19 at 5:37
  • yes , i have followed above command – Rutvee Sojitra Jul 24 '19 at 5:38
  • It seems like there are syntax error of tags. Not sure. Can you please upload your db_schema.xml code? – Rohan Hapani Jul 24 '19 at 5:41
  • already added, i have run above command but db_whitelist_schema.json not generated – Rutvee Sojitra Jul 24 '19 at 5:43
0

As far as I know, it's not possible in magento 2 right now. There is also an issue related to chaning the column type: Table quote column customer_note uses wrong type

This is my fix for that issue:

use Magento\Framework\Setup\Declaration\Schema\Declaration\ReaderComposite;

class ReaderCompositePlugin
{
    public function afterRead(ReaderComposite $subject, $result)
    {
        if (isset($result['table']['quote']) && isset($result['table']['quote']['column']['customer_note'])) {
            $result['table']['quote']['column']['customer_note']['type'] = 'text';
            unset($result['table']['quote']['column']['customer_note']['length']);
        }

        return $result;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.