How To Add Foreign Key In Database Table In Magento 2
The below post gives the solution to add foreign key in database table in Magento 2.
One can establish a relation between two tables with the foreign key.
Add foreign key in a column and establish the relation of that table with another table.
To add foreign key constraint in the database table can be a complex process but not anymore with the solution here:
Method to add foreign key in database table in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 |
addForeignKey( $installer->getFkName( 'child_table', 'main_id', 'main_table', 'entity_id' ), 'main_id', $installer->getTable('main_table'), 'entity_id', \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE ) |
main_table is the name of the parent table.
That’s it.
Any doubts about the implementation? Please mention them in the Comments section below. I’d be happy to help.
Feel free to share the solution via social media profiles with fellow developers.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
Magento 2.3.5 Release – Learn What’s New?
How To Add Quantity Increment And Decrement Button In Magento 2
Next