Why am I unable to create a constraint? The error message doesn’t help much. You just get the following line when you run this SQL statement:
ALTER TABLE user_class ADD FOREIGN KEY (user_id) REFERENCES
Error Code: 1215
Cannot add foreign key constraint
If you try running some of these SQL statements from mysql ( https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html#foreign-keys-adding ), still same error occurred without a reason. You may have to double check a few things within related tables and their fields.
1) The table or index the constraint refers to does not exist yet (usual when loading sql dumps).
2) The table or index in the constraint references misuses quotes.
3) The local key, foreign table or column in the constraint references have a typo.
4) The column the constraint refers to is not of the same type or width as the foreign column
5) The foreign object is not a KEY of any kind
6) The foreign key is a multi-column PK or UK, where the referenced column is not the leftmost one
7) Different charsets/collations among the two table/columns
8) Must be InnoDB
9) Using syntax shorthands to reference the foreign key
10) Using SET DEFAULT for a constraint action