5

In a Magento 2 upgrade script, I wanted to modify (alter) a column to change its comment.

Now that column has a foreign key. Whenever I call the modify column method of the setup class, Mysql reports an error that it can't modify the column because of a foreign key constraint.

What is the correct syntax in the Magento 2 setup-classes API to modify a column's comment where that column has a foreign key constraint?

Code-example of the modify part, type and nullable is exactly the same as for the column when it was created, only the comment is changed. It results in the foreign key constraints error; from a src/app/code/Vendor/ExamplePlugin/Setup/UpgradeSchema.php file:

...

        $connection->modifyColumn(
            $setup->getTable('example_list_item'),
            'list_id',
            [
                'type' => Table::TYPE_INTEGER,
                'nullable' => false,
                'comment' => 'ExampleListID'
            ]
        );

...
2
  • What happens if you try to use changeColumn instead of modifyColumn ? Commented Jun 7, 2016 at 8:32
  • Just a note: The metadata from the entity-manager in magento 2.1 is used in recurring setup scripts to hande "external foreign keys" (externalFK) which's implementation contains some code that might be related. Commented Aug 17, 2016 at 12:14

1 Answer 1

3

I have changed Magento field length using this code. Hope this will help you.

FilePath: [VenderName][ExtensionName]\Setup\InstallSchema.php;

namespace  [VenderName]\[ExtensionName]\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

    class InstallSchema implements InstallSchemaInterface
    {
        public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
        {
            $setup->startSetup();

      $setup->getConnection()->changeColumn(
       $setup->getTable('catalog_product_option_type_price'),
       'price_type',
       'price_type',
       [
        'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
        'length' => 12
       ]
      );
        }
    }
2
  • Is the field you changed part of any foreign key? And how did changing work? Successful or not? And thanks for sharing your hope, it will indeed help me if you provide more details. Commented Aug 27, 2016 at 11:01
  • Yes, the code is working for me and that field is not part of any foreign key. I think you need to change field comment, so it may not be affected even if that field is part of foreign key. Commented Aug 29, 2016 at 3:45

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.