Adding a foreign key constraint to a database table in Magento2 can be a bit confusing. Here’s a simple example of how to create a table with a foreign key to entity_id in the customer_entity table and entity_id in the catalog_product_entity table. These is also an example here of creating an index for your table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
<?php namespace Vendor\ModuleName\Setup; use Magento\Framework\Setup\UpgradeSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterface; use Magento\Framework\DB\Ddl\Table; use Magento\Framework\DB\Adapter\AdapterInterface; /** * Class UpgradeSchema * @package Vendor\ModuleName\Setup */ class UpgradeSchema implements UpgradeSchemaInterface { public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context) { $installer = $setup->startSetup(); if (version_compare($context->getVersion(), '0.0.2') < 0) { $this->createTableWithForeignKeys($installer); } } /** * Create tables for keeping track of many of each product a customer has on backorder * @param SchemaSetupInterface $installer */ protected function createTableWithForeignKeys(SchemaSetupInterface $installer) { $table = $installer->getConnection() ->newTable('my_foreign_key_table') ->addColumn( 'entity_id', Table::TYPE_INTEGER, null, ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true], 'Backorder Unique entity ID' ) ->addColumn( 'product_id', Table::TYPE_INTEGER, null, ['unsigned'=>true, 'nullable'=>false, 'default' => '0'], 'Product Id' ) ->addColumn( 'customer_id', Table::TYPE_INTEGER, null, ['unsigned'=>true, 'nullable'=>false, 'default' => '0'], 'Customer Id' ) ->addIndex( $installer->getIdxName( 'my_foreign_key_table', ['customer_id','product_id'], AdapterInterface::INDEX_TYPE_UNIQUE ), ['customer_id','product_id'], AdapterInterface::INDEX_TYPE_UNIQUE ) ->addForeignKey( // Add foreign key for table entity $installer->getFkName( 'my_foreign_key_table', // New table 'product_id', // Column in New Table 'catalog_product_entity', // Reference Table 'entity_id' // Column in Reference table ), 'product_id', // New table column $installer->getTable('catalog_product_entity'), // Reference Table 'entity_id', // Reference Table Column // When the parent is deleted, delete the row with foreign key Table::ACTION_CASCADE ) ->addForeignKey( // Add foreign key for table entity $installer->getFkName( 'my_foreign_key_table', // New table 'customer_id', // Column in New Table 'customer_entity', // Reference Table 'entity_id' // Column in Reference table ), 'customer_id', // New table column $installer->getTable('customer_entity'), // Reference Table 'entity_id', // Reference Table Column // When the parent is deleted, delete the row with foreign key Table::ACTION_CASCADE ) ->setComment('A comment about your table'); $installer->getConnection()->createTable($table); } } |