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'
            ]
        );

...
  • What happens if you try to use changeColumn instead of modifyColumn ? – Raphael at Digital Pianism Jun 7 '16 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. – hakre Aug 17 '16 at 12:14
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
       ]
      );
        }
    }
  • 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. – hakre Aug 27 '16 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. – user41152 Aug 29 '16 at 3:45

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.