{"id":952,"date":"2020-04-17T12:44:53","date_gmt":"2020-04-17T12:44:53","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/04\/17\/add-foreign-key-in-database-table-in-magento-2\/"},"modified":"2025-05-22T13:29:56","modified_gmt":"2025-05-22T07:59:56","slug":"add-foreign-key-in-database-table-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-foreign-key-in-database-table-in-magento-2\/","title":{"rendered":"How To Add Foreign Key In Database Table In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The below post gives the solution to&nbsp;<em><strong>add foreign key in database table in Magento 2<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One can establish a relation between two tables with the foreign key.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add foreign key in a column and establish the relation of that table with another table.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To add foreign key constraint in the database table can be a complex process but not anymore with the solution here:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to add foreign key in database table in Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">addForeignKey(\n               $installer->getFkName(\n                   'child_table',\n                   'main_id',\n                   'main_table',\n                   'entity_id'\n               ),\n               'main_id',\n               $installer->getTable('main_table'),\n               'entity_id',\n               \\Magento\\Framework\\DB\\Ddl\\Table::ACTION_CASCADE\n           )<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here&nbsp;<em><strong>child_table<\/strong><\/em>&nbsp;is a name of the foreign key table.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">main_table is the name of the parent table.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution via social media profiles with fellow developers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The below post gives the solution to&nbsp;add foreign key in database table in Magento 2. One can establish a relation between two tables with the&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-952","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=952"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/952\/revisions"}],"predecessor-version":[{"id":14962,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/952\/revisions\/14962"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}