0

I have added a product_id column in my table vaibhav_customtabs_product_entity. Now I am trying to add foreign key on product_id from catalog_product_entity->entity_id column, but it is throwing error. Below is my code for add foreign key.

<?php

$installer = $this;
$installer->startSetup();

$installer->getConnection()
->addForeignKey(
 $installer->getFkName('vaibhav_customtabs/product_entity', 'product_id', 'catalog/product','entity_id'),
 'product_id',
 $installer->getTable('catalog/product'), 
 'entity_id',
 Varien_Db_Ddl_Table::ACTION_CASCADE, 
 Varien_Db_Ddl_Table::ACTION_CASCADE
);

$installer->endSetup();

Error

Error in file: "/Applications/XAMPP/xamppfiles/htdocs/store1/app/code/local/Vaibhav/Customtabs/sql/vaibhav_customtabs_setup/upgrade-1.0.0.1-1.0.0.2.php" - SQLSTATE[42S02]: Base table or view not found: 1146 Table 'store1.product_id' doesn't exist

Do I have to add foreign key or alter the column product_id with foreign key as I have already created the column.

1
  • Post your module config file code here Mar 5, 2016 at 9:14

1 Answer 1

0

Try this:

<?php
$installer = $this;
$installer->startSetup();
$installer->getConnection()
    ->addForeignKey(
        $installer->getFkName('vaibhav_customtabs_product_entity', 'product_id', 'catalog_product_entity','entity_id'),
        $installer->getTable('vaibhav_customtabs_product_entity'),
        $installer->getTable('catalog_product_entity'), 
        'entity_id',
        Varien_Db_Ddl_Table::ACTION_CASCADE, 
        Varien_Db_Ddl_Table::ACTION_CASCADE
    )  
$installer->endSetup();
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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