O
O
Outsider V.2019-01-10 12:40:35
Magento
Outsider V., 2019-01-10 12:40:35

Why is the column in the table not deleted when db_schema.xml is changed?

Described the table in a declarative schema (Magento 2.3):
app/code/Mi/DeclarativeSchema/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="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>

Made setup:upgrade, the table was created, everything is OK. I add columns, I do an upgrade - they are added. But when I delete columns from xml, they are not deleted in the table.
Here https://devdocs.magento.com/guides/v2.3/extension-... it is written that it is enough to simply delete or comment out the column line.
Magento connects to the database as root.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
imdeveloper, 2019-01-11
@Audiophile

Try it like I did:
1) create a db_schema.xml file with the following structure:

<?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_new" resource="default" comment="Declarative schema test">
        <column xsi:type="int" name="id" identity="true" comment="ID Auto Increment" />
        <column xsi:type="text" name="name" comment="name" />
        <column xsi:type="text" name="email" comment="email" />
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="id" />
        </constraint>
    </table>
</schema>

2) Clear cache php bin/magento cache:flush
3) Generate db_schema_whitelist.json file using command
php bin/magento setup:db-declaration:generate-whitelist --module-name=Mi_DeclarativeSchema

3) Run upgrade php bin/magento setup:upgrade
After that, a new table will be created with the columns that we specified:
4) Next, comment out the column we don't need:
<?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_new" resource="default" comment="Declarative schema test">
        <column xsi:type="int" name="id" identity="true" comment="ID Auto Increment" />
        <column xsi:type="text" name="name" comment="name" />
        <!--<column xsi:type="text" name="email" comment="email" />-->
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="id" />
        </constraint>
    </table>

5) clear the cache just in case
6) run the whitelist generation again.
php bin/magento setup:db-declaration:generate-whitelist --module-name=Mi_DeclarativeSchema

7) now we run upgrade
and voila , our column has been deleted:
The bottom line is that you need to generate a whitelist after each change in the database, you can read more about this in the documentation. But this is a temporary solution (whitelist) and there will be something new soon, but for now, like this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question