0

I am trying to give foreign keys in Upgrade Schema but it is still not working

class UpgradeSchema implements UpgradeSchemaInterface
{
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        if (version_compare($context->getVersion(), '1.0.2', '<'))
        {
            $setup->getConnection()->modifyColumn(
                $setup->getTable('faq_data'),
                'cat_id',
                ['type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 'nullable' => false]);

            $setup->getConnection()->addForeignKey(
                $setup->getFkName('faq_data', 'cat_id', $setup->getTable('faq_cat_data'), 'cat_id'),
                $setup->getTable('faq_data'),
                'cat_id',
                $setup->getTable('faq_cat_data'),
                'cat_id',
                \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE);
        }
        $setup->endSetup();
    }
}
  • share your error log – Kanhaiya lal May 8 '18 at 7:30
  • there is a nothing in error log – Dipak Solanki May 8 '18 at 7:36
  • @DipakSolanki, you make confuse us.Posted foreign key error bit donot tell what the error.Please explain ther error in brief – Amit Bera May 8 '18 at 7:47
  • it's not assign foreign key that's my problem there is no such error – Dipak Solanki May 8 '18 at 7:51
1

You can try this.

->addForeignKey(
  $installer->getFkName('<ChildTable>', 'entity_id', '<ParentTable>', 'entity_id'),
  'entity_id',
  $installer->getTable('<ParentTable>'),
  'entity_id',
  \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE)
  • Make sure both table have same field.
  • Set primary key to parent table.
  • Change module version in XML file
  • Run php bin/magento setup:upgrade

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.