MySQL Error Code 1215: "Cannot add foreign key constraint"

April 6, 2017
Author
marcos.albe
Share this Post:


MySQL Error Code 1215

Updated 7-05-2019

In this blog, we’ll look at how to resolve MySQL error code 1215: “Cannot add foreign key constraint”.

This error often appears with little context:

There are many possible causes. This guide covers the most common ones, how to diagnose them, and how to fix them.

Tip: Start by checking SHOW ENGINE INNODB STATUS and reviewing the LATEST FOREIGN KEY ERROR section.

1) Referenced table does not exist

Fix: Create the missing table or disable checks temporarily:

2) Incorrect quoting

3) Typos in table or column names

Fix: Verify with SHOW TABLES and SHOW COLUMNS.

4) Column type mismatch

5) Referenced column is not indexed

6) Referenced column not leftmost in composite index

7) Charset/collation mismatch

Fix: Ensure both tables/columns use the same charset and collation.

8) Parent table not using InnoDB

9) Missing column in REFERENCES

10) Parent table is partitioned

11) Referencing a virtual/generated column

12) Using SET DEFAULT

Fix: Remove or replace SET DEFAULT.

13) SET NULL on NOT NULL column

Conclusion

Many causes of error 1215 are due to MySQL limitations rather than mistakes. Understanding these constraints is key to resolving the issue.

More details: Foreign Key restrictions


Get 24/7 Database Support for MySQL today!

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Far
Enough.

Said no pioneer ever.
MySQL, PostgreSQL, InnoDB, MariaDB, MongoDB and Kubernetes are trademarks for their respective owners.
© 2026 Percona All Rights Reserved