简体   繁体   中英

Foreign Key Error - ERROR 1005 (HY000) … Can't create table … `stored_on` (errno: 150)

I'm creating two tables in mariadb. The stored_on relation ties a shelf and a book together. Everything works fine except I added lib_floor to the stored on relation, and this error is suddenly appearing. When I remove lib_floor, the foreign key definition for lib_floor, and lib_floor from stored on primary key, there are no issues. Any ideas? The data type is the same (INT).

CREATE TABLE shelf (
    shelf_number        INT            NOT NULL,
    lib_name            VARCHAR(50)    NOT NULL,
    lib_floor           INT            NOT NULL,
    FOREIGN KEY (lib_name) REFERENCES library (lib_name) ON DELETE CASCADE,
    PRIMARY KEY (shelf_number,lib_name,lib_floor)
);  

CREATE TABLE stored_on (    
    shelf_number      INT            NOT NULL,
    lib_name          VARCHAR(50)    NOT NULL,
    lib_floor         INT            NOT NULL,
    isbn              VARCHAR(20)    NOT NULL,
    total_copies      INT            NOT NULL,
    FOREIGN KEY (shelf_number) REFERENCES shelf (shelf_number) ON DELETE CASCADE,
    FOREIGN KEY (lib_name) REFERENCES shelf (lib_name) ON DELETE CASCADE,
    FOREIGN KEY (lib_floor) REFERENCES shelf (lib_floor) ON DELETE CASCADE,
    FOREIGN KEY (isbn)  REFERENCES book (isbn) ON DELETE CASCADE,
    PRIMARY KEY (shelf_number,lib_name,lib_floor,isbn)
);

Warning 1:

| Warning | 150 | Create table... . stored_on with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns near 'FOREIGN KEY (lib_floor) REFERENCES shelf (lib_floor) ON DELETE CASCADE, FOREIGN KEY (isbn) REFERENCES book (isbn) ON DELETE CASCADE, PRIMARY KEY (shelf_number,lib_name,lib_floor,isbn) )'. |

Error 1:

| Error | 1005 | Can't create table... stored_on (errno: 150 "Foreign key constraint is incorrectly formed")

Warning 2:

Warning | 1215 | Cannot add foreign key constraint for stored_on

The error message is fairly self-explanatory, shelf needs to have an INDEX in which lib_floor is the first field mentioned. You can simply add an INDEX (it doesn't need to be UNIQUE ):

CREATE TABLE shelf (
    shelf_number        INT            NOT NULL,
    lib_name            VARCHAR(50)    NOT NULL,
    lib_floor           INT            NOT NULL,
    FOREIGN KEY (lib_name) REFERENCES library (lib_name) ON DELETE CASCADE,
    PRIMARY KEY (shelf_number,lib_name,lib_floor),
    INDEX (lib_floor)
); 

Note that you would also get this error from lib_name , but the FOREIGN KEY declaration for lib_name in shelf automatically creates an index on it.

Demo on dbfiddle

Foreign keys are tricky. There are several workarounds for err 150 (and others)

  • Disable FKs, CREATE TABLEs, re-enable FKs

  • Apply the FKs via ALTER after doing the relevant CREATE TABLEs

  • (others?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM