MySQL Creating tables with Foreign Keys giving errno: 150

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

提交回复
热议问题