문제

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
도움이 되었습니까?

해결책

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

다른 팁

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.

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top