I am trying to forward engineer my new schema onto my db server, but I can\'t figure out why I am getting this error. I\'ve tried to search for the answer here, but everyth
I had the same problem.
I solved it doing this:
I created the following line in the
primary key: (id int(11) unsigned NOT NULL AUTO_INCREMENT)
I found out this solution after trying to import a table in my schema builder. If it works for you, let me know!
Good luck!
Felipe Tércio
For me, the 1215 error occurred when I was importing a dumpfile created by mysqldump
, which creates the tables alphabetically, which in my case, caused foreign keys to reference tables created later in the file. (Props to this page for pointing it out: https://www.percona.com/blog/2017/04/06/dealing-mysql-error-code-1215-cannot-add-foreign-key-constraint/)
Since mysqldump orders tables alphabetically and I did not want to change the names of tables, I followed the instructions in the answer by JeremyWeir on this page, which states to put set FOREIGN_KEY_CHECKS = 0;
at the top of the dump file and put SET FOREIGN_KEY_CHECKS = 1;
at the bottom of the dump file.
That solution worked for me.
For MySQL (INNODB) ... get definitions for columns you want to link
SELECT * FROM information_schema.columns WHERE
TABLE_NAME IN (tb_name','referenced_table_name') AND
COLUMN_NAME IN ('col_name','referenced_col_name')\G
compare and verify both column definitions have
same COLUMN_TYPE(length), same COLATION
could be helpfull to play like
set foreign_key_checks=0;
ALTER TABLE tb_name ADD FOREIGN KEY(col_name) REFERENCES ref_table(ref_column) ON DELETE ...
set foreign_key_checks=1;
This is a subtle version of what has already been said, but in my instance, I had 2 databases (foo and bar). I created foo first and I didn't realize it referenced a foreign key in bar.baz (which wasn't created yet). When I tried to create bar.baz (without any foreign keys), I kept getting this error. After looking around for a while I found the foreign key in foo.
So, long story short, If you get this error, you may have a pre-existing foreign key to the table being created.
Another reason: if you use ON DELETE SET NULL
all columns that are used in the foreign key must allow null values. Someone else found this out in this question.
From my understanding it wouldn't be a problem regarding data integrity, but it seems that MySQL just doesn't support this feature (in 5.7).
Check for table compatibility. For example, if one table is MyISAM
and the other one is InnoDB
, you may have this issue.