Last answered:

11 Nov 2022

Posted on:

10 Nov 2022


Resolved: foreign key constraint

trying to add foreign key constraint but always giving
21:53:13 alter table sales    add foreign key (customer_id) references customers (customer_id) on delete cascade Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'sales_ibfk_1' in the referenced table 'customers' 0.000 sec

1 answers ( 1 marked as helpful)
Posted on:

11 Nov 2022


Hi Yasmine!
Thanks for reaching out.

This could mean that the customers table does not have a primary key. You should make this column a primary key and only afterwards you will be able to create a foreign key which references it.

Hope this helps.

Submit an answer