->addForeignKey(
                $installer->getFkName(
                    'ag_child_table',
                    'main_id',
                    'ag_main_table',
                    'entity_id'
                ),
                'main_id',
                $installer->getTable('ag_main_table'),
                'entity_id',
                \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
            )
Example
Main Table : ag_sales_rep
Foreign Table: admin_user
->addForeignKey(
    $setup->getFkName('admin_user', 'admin_id', 'ag_sales_rep', 'user_id'),
    'admin_id',
    $setup->getTable('ag_sales_rep'),
    'user_id',
    \Magento\Framework\Db\Ddl\Table::ACTION_SET_NULL,
    \Magento\Framework\Db\Ddl\Table::ACTION_CASCADE
)

Here, “ag_child_table” is a name of foreign key table . -> here  admin_user

“ag_main_table” is a name of parent table/reference table. -> here ag_sales_rep

“main_id” is a reference column name of “ag_main_table” table. -> here admin_id

“entity_id” is a foreign key column name of “ag_child_table” table. -> here entity_id

 

Advertisements