[Solved-6 Solutions] MySQL Error 1215: Cannot add foreign key constraint
Error Description:
MySQL Error 1215: Cannot add foreign key constraint
Solution 1:
MySQL never tells us exactly WHY it failed. There’s actually a multitude of reasons this can happen. Th below list is a compendium of the most common reasons why we can get ERROR 1215, how to diagnose our case to find which one is affecting us and potential solutions for adding the foreign key.
1) The Table or Index the Constraint Refers to Does Not Exist yet (Usual When Loading Dumps)
How to diagnose: Run SHOW TABLES or SHOW CREATE TABLE for each of the parent tables. If we get error 1146 for any of them, it means tables are being created in wrong order.
How to fix: Run the missing CREATE TABLE and try again, or temporarily disable foreign-key-checks. This is especially needed during backup restores where circular references might exist. Simply run:
Example:
2) The Table or Index in the Constraint References Misuses Quotes
How to diagnose: Inspect each FOREIGN KEY declaration and make sure we either have no quotes around object qualifiers, or that we have quotes around the table and a SEPARATE pair of quotes around the column name.
How to fix: Either don’t quote anything, or quote the table and the column separately.
Example:
3) The Local Key, Foreign Table or Column in the Constraint References Have a Typo
How to diagnose: Run SHOW TABLES and SHOW COLUMNS and compare strings with those in our REFERENCESdeclaration.
How to fix: Fix the typo once we find it.
Example:
4) The Column the Constraint Refers to Is Not of the Same Type or Width as the Foreign Column
How to diagnose: Use SHOW CREATE TABLE parent to check that the local column and the referenced column both have same data type and width.
How to fix: Edit our DDL statement such that the column definition in the child table matches that of the parent table.
Example:
5) The Foreign Object Is Not a KEY of Any Kind
How to diagnose: Use SHOW CREATE TABLE parent to check that if the REFERENCES part points to a column, it is not indexed in any way.
How to fix: Make the column a KEY, UNIQUE KEY or PRIMARY KEY on the parent.
Example:
6) The Foreign Key Is a Multi-Column PK or UK, Where the Referenced Column Is Not the Leftmost One
How to diagnose: Do a SHOW CREATE TABLE parent to check if the REFERENCES part points to a column that is present in some multi-column index(es), but is not the leftmost one in its definition.
How to fix: Add an index on the parent table where the referenced column is the leftmost (or only) column.
Example:
7) Different Charsets/Collations Among the Two Table/Columns
How to diagnose: Run SHOW CREATE TABLE parent and compare that the child column (and table) CHARACTER SET and COLLATE parts match those of the parent table.
How to fix: Modify the child table DDL so that it matches the character set and collation of the parent table/column (or ALTER the parent table to match the child’s wanted definition.
Example:
8) The Parent Table Is Not Using InnoDB
How to diagnose: Run SHOW CREATE TABLE parent and verify if ENGINE=INNODB or not.
How to fix: ALTER the parent table to change the engine to InnoDB.
Example:
9) Using Syntax Shorthands to Reference the Foreign Key
How to diagnose: Check if the REFERENCES part only mentions the table name, MySQL doesn’t support this shortcut (even though this is valid SQL).
How to fix: Edit the child table DDL so that it specifies both the table and the column.
Example:
10) The Parent Table Is Partitioned
How to diagnose: Run SHOW CREATE TABLE parent and find out if it’s partitioned or not.
How to fix: Removing the partitioning (i.e., merging all partitions back into a single table) is the only way to get it working.
Example:
11) Referenced Column Is a Generated Virtual Column (This Is Only Possible With 5.7 and Newer)
How to diagnose: Run SHOW CREATE TABLE parent and verify that the referenced column is not a virtual column.
How to fix: CREATE or ALTER the parent table so that the column will be stored and not generated.
Example:
12) Using SET DEFAULT for a Constraint Action
How to diagnose: Check our child table DDL and see if any of our constraint actions (ON DELETE, ON UPDATE) try to use SET DEFAULT
How to fix: Remove or modify actions that use SET DEFAULT from the child table CREATE or ALTERstatement.
Example:
Solution 2:
Error Message:
Example:
Possible Reason:
Case 1: MySQL storage engine.
MySQL supports several storage engines, comparing features of the different mysql storage engines are given below. Note that only InnoDB storage engine supports foreign key, when we are using different mysql storage engine we may get the error code: 1215 cannot add foreign key constraint.
Learn mysql - mysql tutorial - mysql address - mysql examples - mysql programs
Case 2: Key does not exist in the parent table.
When we are trying to reference a key on the parent table which is not exist, we may likely get the error code: 1215 cannot add foreign key constraint. When we are trying to reference a key on parent table which is not a candidate key (either a primary key or a unique key) we may get the error code: 1215 cannot add foreign key constraint. According to definition a foreign key must reference a candidate key of some table. It does not necessarily to be a primary key. MySQL requires index on corresponding referenced key, so we need a unique key.
Case 3: Foreign key definition.
When the definition of the foreign key is different from the reference key, we may get the error code: 1215 cannot add foreign key constraint. The size and sign of the integer must be the same. The character string columns, the character set and collation must be the same. Otherwise we may get the error code: 1215 cannot add foreign key constraint. The length of the string types need not be the same.
Case 4: Foreign key as a primary key.
When we are using composite primary key or implementing one-to-one relationships we may using foreign key as a primary key in our child table. In that case, definition of foreign key should not define as ON DELETE SET NULL. Since primary key cannot be NULL, defining the referential action in such a way may produce the error code: 1215 cannot add foreign key constraint.
Case 5: Referential action - SET NULL.
When we specify SET NULL action and we defined the columns in the child table as NOT NULL, we may get the error code: 1215 cannot add foreign key constraint.
Solution 3:
Case 1: Storage Engine.
Only MySQL storage engine InnoDB supports foreign key, make sure we are using InnoDB storage engine. We can use the following command to determine which storage engine our server supports.
To determine the storage engine used in the corresponding table, we can run the following command:
MySQL allows us to define storage engine on table level, we can assign the storage engine by using the following statement:
Example:
To alter the storage engine of an existing table, we can run the following statement:
Case 2: Key does not exist in the parent table.
Make sure our parent table contains at least one key to which we are going to create a reference key.
we can use the following statement to check the definition of a table:
If the key does not present in the table, we can create a new key by using following statement:
If our table does not have unique column, create a new unique data field and set it as unique so that we can have our existing data by using the following statement:
If the existing table contains the unique data field we can assign it as unique key, by using following statement:
Case 3: Foreign key definition.
The data type must be same for both the foreign key and referenced key. The size and sign of integer types must be the same. For character strings the character set and collation must be the same.
Consider the following example to understand this case:
Note that, in the above example, the data type of the id in student table is TINYINT but the data type of the student_id column in book table which referencing the student table.
Here we need to alter the data type of the student_id column in book table. we can alter the data type of the existing column using following statement:
After altering the required fields, the new statement may look as follows:
Case 4: Foreign key as a primary key.
When we are implementing one-to-one relationship or composite primary key we may use foreign key as a primary key in our child table. Definition of foreign key should not define as ON DELETE SET NULL. Since primary key cannot be NULL. The following example will illustrate this case better:
Case 5: Referential action - SET NULL.
Make sure when we specify SET NULL action, define the columns in the child table as NOT NULL.
The following example will explain this case clearly:
we can solve this by altering the foreign key column from not null to null. we can do that by using following statement:
After modifying the table, the new statement may look similar to as follows:
Solution 4:
Reasons we may get a foreign key constraint error:
We are not using InnoDB as the engine on all tables.
We 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)
The types of the columns are not the same (exception is the column on the referencing table can be nullable).
Update:
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.
Solution 5:
Error 1215 is an annoying one. Explosion Pill's answer covers the basics. we want to make sure to start from there. However, there are more, much more subtle cases to look out for:
For example, when we try to link up PRIMARY KEYs of different tables, make sure to provide proper ON UPDATE and ON DELETE options. E.g.:
PRIMARY KEYs (such as id) can't be NULL.
There are even more, similarly subtle issues when adding these sort of constraints, which is why when coming across constraint errors, always make sure that the constraints and their implications make sense in our current context.
Solution 6:
For MySQL (INNODB) ... get definitions for columns we want to link
compare and verify both column definitions have
same COLUMN_TYPE(length), same COLATION
Could be helpfull to play like
Related Searches to MySQL Error 1215: Cannot add foreign key constraint