1

I have added a table using declarative schema given below:

<table name="trade_item_brand_category">
        <column xsi:type="int" name="id" padding="10" unsigned="true" nullable="false" identity="true" comment="ID"/>
        <column xsi:type="varchar" name="item_pool" nullable="false" length="100" comment="Brand"/>
        <column xsi:type="datetime" name="new_brand_approved_time" on_update="false" nullable="false" comment="Brand Approval Time"/>
        
        <constraint xsi:type="primary" referenceId="PRIMARY">
           <column name="id"/>
        </constraint>
</table>

Now I want to change nullable definition of new_brand_approved_time column from false to true. I have modified this db_schema.xml file as give below

 <column xsi:type="datetime" name="new_brand_approved_time" nullable="true" comment="Brand Approval Time"/>
            

After running setup:upgrade I am checking new_brand_approved_time field but nullable is still false for this column.

I have also tried the old M2 approach of UpgradeSchema as given below, still nullable option is not changing

<?php

namespace Vendor\Module\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\DB\Ddl\Table;

class UpgradeSchema implements UpgradeSchemaInterface
{

    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();
        if ($installer->getConnection()->tableColumnExists('trade_item_brand_category', 'new_brand_approved_time')) {
            $definition = [
                'nullable' => true,
            ];
            $installer->getConnection()->modifyColumn(
                $setup->getTable('trade_item_brand_category'),
                'new_brand_approved_time',
                $definition
            );
        }
    }
}

When I run setup:upgrade, db_schema.xml file is being executed. I have testing it by adding a new column to the table but nullable option is not changing for column new_brand_approved_time.

0

You have to generate whitelist.json after changing db_schema

Using below command:

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

Then check you etc folder if or not db_schema_whitelist.json is generated

1
  • Unfortunately it did not work for me. I have found the solution and added here.
    – amitshree
    Nov 16 at 8:01
0

I had to update setup_version value in etc/module.xml for UpgradeSchema file to execute.

I have done below changes and ran setup:upgrade and it worked.

Updated setup_version from 1.0.0 to 1.0.1

Updated db_schema as given below

<column xsi:type="datetime" name="new_brand_approved_time" nullable="true" comment="Brand Approval Time"/>

And updated UpgradeSchema as given below:

<?php

namespace Unioncoop\Tradeproducts\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\DB\Ddl\Table;

class UpgradeSchema implements UpgradeSchemaInterface
{
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();
        if ($installer->getConnection()->tableColumnExists('trade_item_brand_category', 'new_brand_approved_time')) {
            $definition = [
                'type' => Table::TYPE_DATETIME,
                'nullable' => true
            ];
            $installer->getConnection()->modifyColumn(
                $setup->getTable('trade_item_brand_category'),
                'new_brand_approved_time',
                $definition
            );
        }
    }
}
 
0

I don't think mix db_schema with setup script is a good idea. Your problem maybe come from your data.

You can try run command for filling all data before re-change nullable

example update trade_item_brand_category set new_brand_approved_time = CURRENT_TIMESTAMP where 1=1

and then, change nullable to true and run s:up command

Hope it help!

3
  • Since nullable is false, all existing rows already has value for this column. Now I was trying to make nullable true, so that new rows which will be inserted can have null value.
    – amitshree
    Nov 16 at 10:07
  • yes, i understand. i tried with some field and it fine. Did you face any error when run s:up command Nov 16 at 10:40
  • Nope. No error.
    – amitshree
    Nov 16 at 10:51

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.