6

I described the table with declarative schema (Magento 2.3):

<?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="mi_declarative_schema_test" resource="default" comment="Declarative schema test">
        <column xsi:type="int" name="id" identity="true" comment="ID Auto Increment" />
        <column xsi:type="varchar" name="name" length="1024" nullable="false" comment="Name" />
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="id" />
        </constraint>
    </table>
</schema>

I did setup:upgrade, the table was created, everything is ok. I add the columns,do an upgrade - they are added ok. But when I delete columns from xml, they are not deleted from the table. The instruction https://devdocs.magento.com/guides/v2.3/extension-dev-guide/declarative-schema/db-schema.html says it is enough to delete or comment out the column line. Magento connects to the DB with root access

3 Answers 3

2

I have added delete column sample code for name column. Magento added delete column for disabled="true" .

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="mi_declarative_schema_test" resource="default" comment="Declarative schema test">
    <column xsi:type="int" name="id" identity="true" comment="ID Auto Increment" />
    <column xsi:type="varchar" name="name" length="1024" nullable="false" comment="Name" disabled="true"/>
    <constraint xsi:type="primary" referenceId="PRIMARY">
        <column name="id" />
    </constraint>
</table>
</schema>
1
  • Documentation says "The following example removes the date_closed column by deleting its column node". disabled=true is for deleting column from another module. Anyway, both of these methods don't delete the column from MySQL table
    – Audiophile
    Commented Jan 10, 2019 at 11:04
2

The problem was in whitelist https://devdocs.magento.com/guides/v2.3/extension-dev-guide/declarative-schema/migration-commands.html#create-whitelist

You have to regenerate whitelist for your module every time you delete something in your XML.

0

You need to do the whitelist for your module. You can do this by below commands

php bin/magento setup:db-declaration:generate-whitelist --module-name=[Vendor_Module]

And then after run upgrade command.

php bin/magento setup:upgrade

Thanks

Your Answer

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

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