Delete an existing Foreign key Magento2 installer

If you want to Delete an existing foreign key from a table by installer, then you can use following code:

<?php

namespace Webkul\Test\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**
 * @codeCoverageIgnore
 */
class UpgradeSchema implements UpgradeSchemaInterface
{
    /**
     * {@inheritdoc}
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $setup->getConnection()->dropForeignKey(
            $setup->getTable('wk_child_table'),
            $setup->getFkName(
                'wk_child_table',
                'entity_id',
                'wk_main_table',
                'main_id'
            )
        );

        $setup->endSetup();
    }
}

Here,

wk_child_table is the table name in which you created a foreign key column.

wk_main_table is the table which you used for reference.

entity_id is the foreign key column name of  “wk_child_table”.

main_id is the referenced column of table “wk_main_table”.

 

If you want to add a foreign key in your table then refer following blog:

Add Foreign Key In Magento2

 

Hope it will help you.

Thank you.

. . .

Comment

Add Your Comment

Be the first to comment.

css.php
Hire Us!
Brief us about your requirements and we'll get back to you.
Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again
Close