MySQL Creating tables with Foreign Keys giving errno: 150

前端 未结 20 2408
深忆病人
深忆病人 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:17

    execute below line before creating table : SET FOREIGN_KEY_CHECKS = 0;

    FOREIGN_KEY_CHECKS option specifies whether or not to check foreign key constraints for InnoDB tables.

    -- Specify to check foreign key constraints (this is the default)

    SET FOREIGN_KEY_CHECKS = 1;
    

     

    -- Do not check foreign key constraints

    SET FOREIGN_KEY_CHECKS = 0;

    When to Use : Temporarily disabling referential constraints (set FOREIGN_KEY_CHECKS to 0) is useful when you need to re-create the tables and load data in any parent-child order

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

    What's the current state of your database when you run this script? Is it completely empty? Your SQL runs fine for me when creating a database from scratch, but errno 150 usually has to do with dropping & recreating tables that are part of a foreign key. I'm getting the feeling you're not working with a 100% fresh and new database.

    If you're erroring out when "source"-ing your SQL file, you should be able to run the command "SHOW ENGINE INNODB STATUS" from the MySQL prompt immediately after the "source" command to see more detailed error info.

    You may want to check out the manual entry too:

    If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message. If MySQL reports an error number 1005 from a CREATE TABLE statement, and the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed.

    — MySQL 5.1 reference manual.

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

    For others that find this SO entry via Google: Be sure that you aren't trying to do a SET NULL action on a foreign key (to be) column defined as "NOT NULL." That caused great frustration until I remembered to do a CHECK ENGINE INNODB STATUS.

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

    MySQL Workbench 6.3 for Mac OS.

    Problem: errno 150 on table X when trying to do Forward Engineering on a DB diagram, 20 out of 21 succeeded, 1 failed. If FKs on table X were deleted, the error moved to a different table that wasn't failing before.

    Changed all tables engine to myISAM and it worked just fine.

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

    (Side notes too big for a Comment)

    There is no need for an AUTO_INCREMENT id in a mapping table; get rid of it.

    Change the PRIMARY KEY to (role_id, role_group_id) (in either order). This will make accesses faster.

    Since you probably want to map both directions, also add an INDEX with those two columns in the opposite order. (There is no need to make it UNIQUE.)

    More tips: http://mysql.rjweb.org/doc.php/index_cookbook_mysql#speeding_up_wp_postmeta

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

    I've found another reason this fails... case sensitive table names.

    For this table definition

    CREATE TABLE user (
      userId int PRIMARY KEY AUTO_INCREMENT,
      username varchar(30) NOT NULL
    ) ENGINE=InnoDB;
    

    This table definition works

    CREATE TABLE product (
      id int PRIMARY KEY AUTO_INCREMENT,
      userId int,
      FOREIGN KEY fkProductUser1(userId) REFERENCES **u**ser(userId)
    ) ENGINE=InnoDB;
    

    whereas this one fails

    CREATE TABLE product (
      id int PRIMARY KEY AUTO_INCREMENT,
      userId int,
      FOREIGN KEY fkProductUser1(userId) REFERENCES User(userId)
    ) ENGINE=InnoDB;
    

    The fact that it worked on Windows and failed on Unix took me a couple of hours to figure out. Hope that helps someone else.

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