0

I'm trying to add a foreign key of the customer in my table in InstallSchema file :

$installer->getConnection()
                ->addColumn($installer->getTable('fmj_crf/projet'), 'customer_fk_id', Table::TYPE_INTEGER, null, array(
                    'unsigned' => true,
                    'nullable' => false,
                        ), 'customer Id');

        $installer->getConnection()->addForeignKey(
                $installer->getFkName('fmj_crf/projet', 'customer_fk_id', 'customer_entity', 'entity_id'), $installer->getTable('fmj_crf/projet'), 'customer_fk_id', $installer->getTable('customer_entity'), 'entity_id'
        );

but when I upgrade it gives me this error :

[PDOException]                                                                                             
  SQLSTATE[HY000]: General error: 1825 Failed to add the foreign key constraint on table 'fmj_crf/projet'.   
  Incorrect options in FOREIGN KEY constraint 'test2/FMJ_CRF/PROJET_CUSTOMER_FK_ID_CUSTOMER_ENTITY_ENTITY_I  
  D'

Anyone can help ?

  • whats your custom table name? – Rakesh Jesadiya Oct 10 '17 at 12:04
  • the name is fmj_crf/projet – Ayoub El Hayat Oct 10 '17 at 12:05
  • Are you using upgrade schema or Install Schema. In your question you have mentioned use of InstallSchema and In below you have written when upgrade gives you error. Clarify it. – Priyank Oct 10 '17 at 12:39
  • I'm using InstallSchema , and after I run the command setup:upgrade to enable my module I had the error I mentioned. – Ayoub El Hayat Oct 10 '17 at 12:49
1

I found a solution by changing the definition to the foreign key column in the upgradeSchema file like this :

$setup->getConnection()->changeColumn(
                    'fmj_crf/projet', 'customer_fk_id', 'customer_fk_id', ['type' => Table::TYPE_INTEGER, 'unsigned' => true, 'nullable' => false, 'primary' => true,]
            );

and then :

$setup->getConnection()->addForeignKey(
                    $setup->getFkName(
                            'fmj_crf/projet', 'customer_fk_id', 'customer_entity', 'entity_id'
                    ), $setup->getTable('fmj_crf/projet'), 'customer_fk_id', $setup->getTable('customer_entity'), 'entity_id', \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
            );
1
    $setup->getConnection()
            ->addForeignKey(
                $setup->getFkName(
                    'your_custom_table_name',                   // priTableName
                    'your_custom_table_field',                  // priColumnName
                    'customer_entity',                          // refTableName
                    'entity_id'                                 // refColumnName
                ),
                'your_custom_table_field',                      // column
                $setup->getTable('customer_entity'), 
                'entity_id',                                    // refColumn
                \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE // onDelete

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.