Where the open source database community meets: Use code PERCONA75 and secure your spot for Percona Live.  Register

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

21 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Valeriy
9 years ago

Good post, but it seems one important detail is missing: you may find more details on why adding foreign key failed using SHOW ENGINE INNODB STATUSG, like in this case:

mysql> create table parent(id int primary key);
Query OK, 0 rows affected (0.33 sec)

mysql> CREATE TABLE child (
-> id INT(10) NOT NULL PRIMARY KEY,
-> parent_id INT(10) NOT NULL,
-> FOREIGN KEY (parent_id) REFERENCES parent(id) ON UPDATE SET DEFAULT
-> ) ENGINE INNODB;
ERROR 1215 (HY000): Cannot add foreign key constraint
mysql> show engine innodb statusG
*************************** 1. row ***************************
Type: InnoDB
Name:
Status:
=====================================
2017-04-06 21:26:12 1b38 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 10 seconds
—————–
BACKGROUND THREAD
—————–
srv_master_thread loops: 2 srv_active, 0 srv_shutdown, 129 srv_idle
srv_master_thread log flush and writes: 131
———-
SEMAPHORES
———-
OS WAIT ARRAY INFO: reservation count 7
OS WAIT ARRAY INFO: signal count 7
Mutex spin waits 3, rounds 90, OS waits 3
RW-shared spins 4, rounds 120, OS waits 4
RW-excl spins 0, rounds 0, OS waits 0
Spin rounds per wait: 30.00 mutex, 30.00 RW-shared, 0.00 RW-excl
————————
LATEST FOREIGN KEY ERROR
————————
2017-04-06 21:26:03 1b38 Error in foreign key constraint of table test/child:
FOREIGN KEY (parent_id) REFERENCES parent(id) ON UPDATE SET DEFAULT
) ENGINE INNODB:
Syntax error close to:
DEFAULT
) ENGINE INNODB

Way more informative than just error 1215 🙂

KarLKoX
KarLKoX
9 years ago

Hi,
I just found another way when mySQL fails to create a FK : the columns of the referenced table MUST MATCH the order of the PK !
I didn’t found anything in the web with this restriction.

luke
luke
8 years ago

it works.Thank u very much

Vicky Anand
Vicky Anand
8 years ago

Thanks Amazing details, for me it was character set issue

Michael
Michael
8 years ago

Found another reason, maybe a stupid one, but to me it happened:
If you want the constraint action to be “SET NULL”, then the child column needs to allow NULL-values in the table structure.

Junaid
Junaid
8 years ago

Thanks, thats a great info,

Valeriy – your InnoDB status query helped me to understand why I was getting an error.

Basically, I was trying to ‘SET NULL’ on delete and at the same time I was defining the column as NOT NULL.

Nabil
Nabil
8 years ago

Saved my life bro..Thanks

Barclay Dunn
Barclay Dunn
7 years ago

AWESOME post! I’ve never run across different charsets being the issue before, so you saved my life too!

Fedrick
Fedrick
7 years ago

Very helpful, thanks a lot for the post!

Diego Carvalho
Diego Carvalho
7 years ago

save me with 12°

Aleksandr Mikheev (@ayamikh)

Thank you! You helped me a lot!

Merianos Nikos
7 years ago

Awesome post !! You saved my day 🙂 Thank you very much 🙂

Archie Hughes
Archie Hughes
7 years ago

Thank you for sharing valuable information. Nice post. I enjoyed reading this post. soundcloud downloader

Aray
Aray
6 years ago

Thank you for this very helpful post.

Sławomir B.
6 years ago

#5 got my error fixed – thank you for the detailed post.

Deepsha Lal
Deepsha Lal
5 years ago

So I have this assignment which mandates us to follow the given attribute and table names. Unfortunately, one of the table names is a reserved keyword- ‘Session’. Phpmyadmin doesn’t give any errors while creating the Session table but when I try to create another table that contains a foreign key referencing the Session table, I get the error “cannot add foreign key constraint”. Can you tell me how I should write the name of the session table so that phpmyadmin recognizes it as a table and not a keyword.

Pick Avana
Pick Avana
5 years ago

Great read. Tried all of the above and still unable to create FK Constraint on existing tables. I created two new tables and FK without any problem but Im trying to create FK with two existing tables – both empty. I made sure no typos. I have foreign field indexed. I have same type, width for foreign key and related primary key. Foreign field is not set to NOT NULL. Tried all. Still fails. Could I provide sql dumps of two tables (no records) and see if you spot something?

Pick Avana
Pick Avana
5 years ago

The tables I was able to add a FK for had type INT(11) for Primary and foreign keys. The tables I am unsuccessfully trying to add FK to have BIGINT(20) – could that be the problem?

Pick Avana
Pick Avana
5 years ago

I solved the problem. I found that the primary keys were created using attribute “UNSIGNED” while the foreign key fields did not have that attribute. I change the foreign key fields to “UNSIGNED” and bingo! Thanks again for your article though. It helped tremendously in diving into the details and there it was, as simple as that. Thank you.

Far
Enough.

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