MySQL Error 1215: Cannot add foreign key constraint
Reasons you may get a foreign key constraint error:
- You are not using InnoDB as the engine on all tables.
- You are trying to reference a nonexistent key on the target table. Make sure it is a key on the other table (it can be a primary or unique key, or just a
key
) - The types of the columns are not the same (an exception is the column on the referencing table can be nullable even if it is not nullable in the referenced table).
- If the primary key or foreign key is a varchar, make sure the collation is the same for both.
- One of the reasons may also be that the column you are using for
ON DELETE SET NULL
is not defined to be null. So make sure that the column is set default null.
Check these.
I'm guessing that Clients.Case_Number
and/or Staff.Emp_ID
are not exactly the same data type as Clients_has_Staff.Clients_Case_Number
and Clients_has_Staff.Staff_Emp_ID
.
Perhaps the columns in the parent tables are INT UNSIGNED
?
They need to be exactly the same data type in both tables.