简体   繁体   中英

MySQL Error: Error creating table: Cannot add foreign key constraint

I'm trying to set up a reference between two tables, and I keep running into this error when I load the page.

Error creating table: Cannot add foreign key constraint

This is the 'calendars' table: calendars table image

And this is the current query I am running to create the table:

CREATE TABLE `'.$classTableName.'`(
    classID INT(11) NOT NULL,
    name VARCHAR(255) NOT NULL,
    users INT(11) NOT NULL,
    files INT(11) NOT NULL,
    chats INT(11) NOT NULL,
    deadlines INT(11) NOT NULL,
    calendar INT(11) NOT NULL,
    PRIMARY KEY (classID), 
    FOREIGN KEY (calendar) REFERENCES calendars(classID)
) ENGINE=INNODB;

Any help would be appreciated! :)

正如Basmar指出的那样,必须索引calendars.classID

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