Last answered:

10 Nov 2023

Posted on:

09 Nov 2023

0

Resolved: how to solve this error

0
59
21:45:17
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)
Instructor
Posted on:

10 Nov 2023

0

Hi Magesh!
Thanks for reaching out.


The error suggests that the `customer_id` field in the `customers` table is not indexed. To fix this, you need to create an index on `customer_id` in the `customers` table before you can add a foreign key constraint that references it.


Hope this helps.
Best,
Tsvetelin

Submit an answer