Foreign key issue:ERROR 1005 (HY000): Can't create table (errno: 150)

后端 未结 2 1105
难免孤独
难免孤独 2021-01-26 10:49

I\'m getting this error: ERROR 1005 (HY000): Can\'t create table (errno: 150); I know it has something to do with the foreign keys but I have checked to see if they

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-26 10:58

    You have FK definitions that will try to set a null value on non null columns. Therefore that FK declaration is impossible.

    CREATE TABLE IF NOT EXISTS registration (
      branchNo VARCHAR (15) NOT NULL,
      memberNo VARCHAR (15) **NOT NULL**,
      StaffNo VARCHAR (15) **NOT NULL**,
      dateJoined date NOT NULL,
      PRIMARY KEY (branchNo, memberNo), 
    FOREIGN KEY (memberNo) REFERENCES member (memberNo) ON DELETE **SET NULL** ON UPDATE 
    CASCADE, 
    FOREIGN KEY (StaffNo) REFERENCES staff (StaffNo) ON DELETE **SET NULL** ON UPDATE 
    CASCADE
    ) ENGINE=InnoDB;
    

    you can either set "on DELETE CASCADE" or allow those fields to be null.

    You might have the same error on other tables. I just stopped at the first error I found :)

提交回复
热议问题