3

When I adding a foreign key, I write something like:

->addForeignKey(
    $installer->getFkName('my_table_name', 'website_id', 'store_website', 'website_id'),
    'website_id',
    'store_website',
    'website_id',
    Table::ACTION_CASCADE
)

But when table prefix is used, table name can be different from the base name. What should I do in this case? Should I use the method SchemaSetupInterface::getTable($tablename) or Magento adds table prefix automatically?

PS: with the getTable method it could look like this:

->addForeignKey(
    $installer->getFkName('my_table_name', 'website_id', 'store_website', 'website_id'),
    'website_id',
    $installer->getTable('store_website'), // << CHANGED
    'website_id',
    Table::ACTION_CASCADE
)

1 Answer 1

1

Use of SchemaSetupInterface::getTable($tablename) is the right syntax for the foreign key in InstallScript. If you use that Magento2 checked table prefix if any and add that automatically.

Magento2 team also used that for all its core module.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.