MySQL Creating tables with Foreign Keys giving errno: 150

前端 未结 20 2407
深忆病人
深忆病人 2020-11-21 05:02

I am trying to create a table in MySQL with two foreign keys, which reference the primary keys in 2 other tables, but I am getting an errno: 150 error and it will not create

相关标签:
20条回答
  • 2020-11-21 05:24

    Make sure that the properties of the two fields you are trying to link with a constraint are exactly the same.

    Often, the 'unsigned' property on an ID column will catch you out.

    ALTER TABLE `dbname`.`tablename` CHANGE `fieldname` `fieldname` int(10) UNSIGNED NULL;
    
    0 讨论(0)
  • 2020-11-21 05:25

    When the foreign key constraint is based on varchar type, then in addition to the list provided by marv-el the target column must have an unique constraint.

    0 讨论(0)
  • 2020-11-21 05:26

    MySQL’s generic “errno 150” message “means that a foreign key constraint was not correctly formed.” As you probably already know if you are reading this page, the generic “errno: 150” error message is really unhelpful. However:

    You can get the actual error message by running SHOW ENGINE INNODB STATUS; and then looking for LATEST FOREIGN KEY ERROR in the output.

    For example, this attempt to create a foreign key constraint:

    CREATE TABLE t1
    (id INTEGER);
    
    CREATE TABLE t2
    (t1_id INTEGER,
     CONSTRAINT FOREIGN KEY (t1_id) REFERENCES t1 (id));
    

    fails with the error Can't create table 'test.t2' (errno: 150). That doesn’t tell anyone anything useful other than that it’s a foreign key problem. But run SHOW ENGINE INNODB STATUS; and it will say:

    ------------------------
    LATEST FOREIGN KEY ERROR
    ------------------------
    130811 23:36:38 Error in foreign key constraint of table test/t2:
    FOREIGN KEY (t1_id) REFERENCES t1 (id)):
    Cannot find an index in the referenced table where the
    referenced columns appear as the first columns, or column types
    in the table and the referenced table do not match for constraint.
    

    It says that the problem is it can’t find an index. SHOW INDEX FROM t1 shows that there aren’t any indexes at all for table t1. Fix that by, say, defining a primary key on t1, and the foreign key constraint will be created successfully.

    0 讨论(0)
  • 2020-11-21 05:31

    This is usually happening when you try to source file into existing database. Drop all the tables first (or the DB itself). And then source file with SET foreign_key_checks = 0; at the beginning and SET foreign_key_checks = 1; at the end.

    0 讨论(0)
  • 2020-11-21 05:31

    Also worth checking that you aren't accidentally operating on the wrong database. This error will occur if the foreign table does not exist. Why does MySQL have to be so cryptic?

    0 讨论(0)
  • 2020-11-21 05:33

    Please make sure at first that

    1. you are using InnoDB tables.
    2. field for FOREIGN KEY has the same type and length (!) as source field.

    I had the same trouble and I've fixed it. I had unsigned INT for one field and just integer for other field.

    0 讨论(0)
提交回复
热议问题