Right syntax for the foreign key in InstallScript
-
11-10-2020 - |
Pregunta
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
)
Solución
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.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange