5

I have created Custom Table Using below code. Table has been created successfully with columns.

<?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="custom_table" resource="default" engine="innodb" comment="Custom Table">
        <column xsi:type="int" name="inc_id" padding="11" unsigned="false" nullable="false" identity="true" comment="ID"/>
        <column xsi:type="varchar" name="title" nullable="false" length="255" comment="Title"/>
         <column xsi:type="varchar" name="subtitle" nullable="false" length="255" comment="Subtitle"/>
        <constraint xsi:type="primary" name="PRIMARY">
            <column name="inc_id"/>
        </constraint>
    </table>
</schema>

But when i try to remove the column by removing the subtitle column, It doesn't get deleted.My Updated Xml looks like this,

<?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="custom_table" resource="default" engine="innodb" comment="Custom Table">
        <column xsi:type="int" name="inc_id" padding="11" unsigned="false" nullable="false" identity="true" comment="ID"/>
        <column xsi:type="varchar" name="title" nullable="false" length="255" comment="Title"/>
        <constraint xsi:type="primary" name="PRIMARY">
            <column name="inc_id"/>
        </constraint>
    </table>
</schema>

I have run following command after removing the column from xml,which created db_schema_whitelist.json in etc folder.

$ bin/magento setup:db-declaration:generate-whitelist [options]

Finally upgrade command.

$ bin/magento setup:upgrade
1

3 Answers 3

5

I managed to found it myself, we can delete those column by using disabled="true" property.

<?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="custom_table" resource="default" engine="innodb" comment="Custom Table">
        <column xsi:type="int" name="inc_id" padding="11" unsigned="false" nullable="false" identity="true" comment="ID"/>
        <column xsi:type="varchar" name="title" nullable="false" length="255" comment="Title" disabled="true"/>
         <column xsi:type="varchar" name="subtitle" nullable="false" length="255" comment="Subtitle" disabled="true"/>
        <constraint xsi:type="primary" name="PRIMARY">
            <column name="inc_id"/>
        </constraint>
    </table>
</schema>

After that run the command

$ bin/magento setup:db-declaration:generate-whitelist
$ bin/magento setup:upgrade
0

We can use dropColumn script:

$setup->getConnection()->dropColumn($setup->getTable('your_table'), 'your_column');

doing with UpgradeSchema.php is a better and cleaner solution

Please check.

6
  • can't we remove the column using xml itself?
    – aravind
    Jun 28, 2019 at 9:11
  • I searched, but not found column delete from the xml. This the way I know. You can use.
    – Ravi Soni
    Jun 28, 2019 at 9:25
  • Thanks, I just searched and found out we need to use disabled="true" against column that we need to delete.
    – aravind
    Jun 28, 2019 at 9:26
  • I deleted it by using <column xsi:type="varchar" name="subtitle" nullable="false" length="255" comment="Subtitle" diabled="true"/>
    – aravind
    Jun 28, 2019 at 9:26
  • Ohh, great. you can add it as the answer. So might it will help someone.
    – Ravi Soni
    Jun 28, 2019 at 9:29
0

Try to revert your db_schema.xml file and run $ bin/magento setup:db-declaration:generate-whitelist again, then remove the column, change module version number to a higher one and then run $ bin/magento setup:upgrade.

https://devdocs.magento.com/guides/v2.3/extension-dev-guide/declarative-schema/db-schema.html

3
  • Do we need to change module version? I thought they have mentioned that for previous magento versions where we use installschema , upgradeschema
    – aravind
    Jun 28, 2019 at 9:53
  • I've didn't tested but in logic it is needed.
    – krybbio
    Jun 28, 2019 at 11:37
  • @krybbio no, using db_schema altering the tables will not needed change in module versioning May 26 at 5:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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