ERROR 1215 occurs when MySQL cannot create a foreign key constraint due to mismatched data types, missing indexes, or storage engine issues. The fix involves verifying data types match exactly, ensuring the referenced column has an index, and confirming both tables use InnoDB.
ERROR 1215 (HY000) is triggered when you attempt to create a foreign key constraint, but MySQL cannot establish the relationship between tables. Unlike most errors, MySQL does not specify the exact reason why the constraint failed, making this error particularly frustrating to debug. This error occurs exclusively in InnoDB tables since MyISAM does not support foreign keys. The constraint cannot be created due to structural incompatibilities between the foreign key column and the referenced column it points to.
First, verify that both your parent and child tables use the InnoDB storage engine. Only InnoDB supports foreign key constraints.
SHOW TABLE STATUS WHERE name IN ('parent_table', 'child_table');If the Engine column shows MyISAM, convert the table:
ALTER TABLE child_table ENGINE=InnoDB;
ALTER TABLE parent_table ENGINE=InnoDB;Ensure the referenced parent table exists and contains the column you are referencing. Tables must be created before creating the foreign key constraint.
SHOW CREATE TABLE parent_table;
DESCRIBE parent_table;Verify the column name in the REFERENCES clause matches exactly, including case sensitivity.
The foreign key column and the referenced column must have identical data types, including size and sign.
DESCRIBE parent_table;
DESCRIBE child_table;Common mismatches to watch for:
- INT vs. INT UNSIGNED
- INT vs. BIGINT
- INT vs. SMALLINT
- VARCHAR(100) with different collations
If the types don't match, modify the child table column to match the parent:
ALTER TABLE child_table MODIFY COLUMN child_id INT UNSIGNED;The referenced column must have either a PRIMARY KEY or a UNIQUE index. Check existing indexes:
SHOW INDEX FROM parent_table;If the referenced column doesn't have an index, add one:
ALTER TABLE parent_table ADD UNIQUE KEY uk_parent_id (parent_id);Or if it should be the primary key:
ALTER TABLE parent_table ADD PRIMARY KEY (parent_id);For VARCHAR or TEXT columns, both the foreign key column and referenced column must use the same character set and collation.
SHOW CREATE TABLE parent_table;
SHOW CREATE TABLE child_table;Look for CHARSET and COLLATE in the column definitions. If they differ, modify the child table column:
ALTER TABLE child_table MODIFY COLUMN child_name VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;If the above steps don't resolve the issue, run this diagnostic command to see detailed error information:
SHOW ENGINE INNODB STATUS;Look for the "LATEST FOREIGN KEY ERROR" section, which provides more specific details about why the constraint failed. This output is often more helpful than ERROR 1215 itself.
Once all prerequisites are met (matching data types, valid index on parent column, InnoDB engine), create the constraint:
ALTER TABLE child_table
ADD CONSTRAINT fk_child_parent
FOREIGN KEY (child_id)
REFERENCES parent_table (parent_id)
ON DELETE CASCADE
ON UPDATE CASCADE;Or during table creation:
CREATE TABLE child_table (
child_id INT UNSIGNED NOT NULL,
CONSTRAINT fk_child_parent FOREIGN KEY (child_id)
REFERENCES parent_table (parent_id)
ON DELETE CASCADE
);Partitioned tables cannot be referenced by foreign key constraints; if your parent table uses partitioning, you must remove it by merging all partitions back into a single table. Virtual or generated columns (created with AS expression) cannot be referenced by foreign keys; the column must be a stored column with actual data. When using ON DELETE SET NULL or ON UPDATE SET NULL, the child column must be defined as nullable (without NOT NULL). For bulk operations on large tables, consider using tools like pt-online-schema-change to avoid locking the entire table during index creation or engine conversion. Some MySQL versions have known bugs with foreign key constraints and fulltext indexes on the same table (see MySQL bugs #89219 and #89220); upgrading to the latest MySQL version may resolve such edge cases.
ERROR 1064: You have an error in your SQL syntax
How to fix "ERROR 1064: You have an error in your SQL syntax" in MySQL
ERROR 1054: Unknown column in field list
Unknown column in field list
ER_WINDOW_RANGE_FRAME_NUMERIC_TYPE (3589): RANGE frame requires numeric ORDER BY expression
RANGE frame requires numeric ORDER BY expression in MySQL window functions
CR_ALREADY_CONNECTED (2058): Handle already connected
How to fix "CR_ALREADY_CONNECTED (2058): Handle already connected" in MySQL
ER_WINDOW_DUPLICATE_NAME (3591): Duplicate window name
How to fix ER_WINDOW_DUPLICATE_NAME (3591) in MySQL window functions