Software⏱️ 2 min read📅 2026-05-30

How to Fix Error 1215 Error – MySQL Error 1215: Cannot add foreign key constraint

Quick Answer: Check the data types of the foreign key and primary key columns. Ensure they match, as MySQL requires them to be the same for a foreign key constraint to work.

MySQL Error 1215: Cannot add foreign key constraint occurs when the table being referenced by a foreign key does not exist in the database. This can happen if you are attempting to create a foreign key on a column that is not part of the primary key or unique index of the referenced table.

💡 Why You Are Getting This Error

  • [Cause]

🛠️ Step-by-Step Verified Fixes

Method 1: Check Referenced Table Existence

  1. Step 1: Verify that the table being referenced by the foreign key exists in the database.

Method 2: Check Primary Key and Unique Index

  1. Step 1: Ensure that the column being used as a foreign key is part of the primary key or unique index of the referenced table.

Method 3: Use InnoDB Engine

  1. Step 1: Check if the database engine is set to InnoDB. If not, update the engine using the following query: ALTER DATABASE your_database_name ENGINE=InnoDB;

✨ Wrapping Up

By following these steps, you should be able to resolve the MySQL Error 1215: Cannot add foreign key constraint and successfully forward engineer your new schema onto your database server.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions