cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2.3, Cannot able to edit or delete the Foreign Key Column

Magento 2.3, Cannot able to edit or delete the Foreign Key Column

I have a student table.. with fields entity id(primary key), and student id..

now i have created a field in the customer_entity table as student id..

i have applied foreign key to the student id in customer table using this:

 

->addForeignKey(
    $installer->getFkName('customer_entity', 
        'student_id', 
        'student_table', 
        'student_id'),
    'customer_entity',
    'student_id',
    $installer->getTable('student_table'),
    'student_id'
    ,
    \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE);

 

there is no error.. but now if i am trying to edit or delete the student id in student table it is giving me error..

 

 

 

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`magento`.`customer_entity`, CONSTRAINT `CUSTOMER_ENTITY_STUDENT_ID_STUDENT_TABLE_STUDENT_ID` FOREIGN KEY (`student_id`) REFERENCES `student_table` (`student_id`) ON DELETE CASCADE), query was: UPDATE `student_table` SET `student_id` = ? WHERE (entity_id='1')

 

can anyone help?

4 REPLIES 4

Re: Magento 2.3, Cannot able to edit or delete the Foreign Key Column

Hi @hira_ahmed,

I think you have created foreign key in customer_entity table..

But you need to create foreign key for student table..

Re: Magento 2.3, Cannot able to edit or delete the Foreign Key Column

@Vimal Kumar  this is working.. if i delete the Parent table(student table) student id.. it is deleting the customer's whole row containing that student id.. but i cannot update the student id in the parent table.. why?

Re: Magento 2.3, Cannot able to edit or delete the Foreign Key Column

@hira_ahmed

Please make sure that you have added on update cascade in your parent table with foreign key.

Re: Magento 2.3, Cannot able to edit or delete the Foreign Key Column

@Vimal Kumar 

i had added Action Cascade on the parent table as you can see in my above code

  \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE);