Home > cannot add foreign key constraint

How to fix "Cannot add foreign key constraint" error in MySQL?

Alton Alexander
By Alton AlexanderUpdated on June 3rd, 2022

Cannot add foreign key constraint means that the current table cannot be linked to another table because it does not have a matching key. This error code usually happens when the data in the two tables are not consistent, or when the data types of the keys are different.

Other common searches for this error include:

  • error code: 1215. cannot add foreign key constraint
  • cannot add foreign key constraint

1. Check if the column type of the foreign key matches the column type of the primary key in the parent table

  1. Select the foreign key in the parent table.
  2. Click on the "Properties" button.
  3. Click on the "Constraints" tab.
  4. Check that the "Column type" of the foreign key matches the "Column type" of the primary key in the parent table.

2. Check if the foreign key column is indexed

To check if the foreign key column is indexed, you can use the following query: SELECT COUNT(*) FROM table_name WHERE foreign_key_column IS NOT NULL;

3. Check if the foreign key column has a NOT NULL constraint

To check if the foreign key column has a NOT NULL constraint, you will need to use the following SQL statement: SELECT column_name FROM table_name WHERE NOT NULL If the foreign key column does not have a NOT NULL constraint, then you will need to add a NOT NULL constraint to the column.

4. Check if there are any conflicting constraints on the foreign key column

The first step is to determine if there are any conflicting constraints on the foreign key column. If there are, then the next step is to fix the conflicts.

5. Check if there is already a foreign key constraint on the column

  1. Check the table structure To check the table structure, you can use the information_schema.table_columns table. If the table does not have a foreign key constraint on the column, you can add one by following these steps: a. Select the column that you want to constraint. b. In the SQL editor, type the following command: ALTER TABLE table_name ADD CONSTRAINT constraint_name FOREIGN KEY (column_name) REFERENCES table_other_name c. Press Enter. d. If the constraint is successfully added, the constraint_name column will have a text value that is the name of the constraint. e. If the constraint is not successfully added, you will receive an error message. Check the table structure to find out why the constraint was not added.
  2. Check the data type of the column If the column is not of the data type that you are using for the foreign key, you will need to change the data type of the column. To do this, you can use the information_schema.columns table.
  3. Check the index on the column If the column does not have an index, you will need to create an index. To do this, you can use the information_schema.columns table.
  4. Check the foreign key constraints If the column does have a foreign key constraint, you will need to verify that the constraint is valid. To do this, you can use the information_schema.foreign_keys table.

If you didn't find success with an option above, then try:

  1. Check if the column is a primary key column.
  2. Check if the column is a part of a composite primary key.