2016-01-25 79 views
2

您好我有兩個表:外鍵約束的格式不正確 - mysql的

CREATE TABLE `user` (
`email` VARCHAR(255) NULL DEFAULT NULL, 
`username` VARCHAR(255) NULL DEFAULT NULL, 
`password` VARCHAR(255) NULL DEFAULT NULL, 
`website` INT(11) NULL DEFAULT NULL, 
`facebook_id` VARCHAR(255) NULL DEFAULT NULL, 
`google_id` VARCHAR(255) NULL DEFAULT NULL, 
`provider` VARCHAR(255) NULL DEFAULT NULL, 
`getUpdates` TINYINT(1) NOT NULL, 
`id` VARCHAR(60) NOT NULL, 
`createdAt` DATETIME NULL DEFAULT NULL, 
`updatedAt` DATETIME NULL DEFAULT NULL, 
PRIMARY KEY (`id`), 
UNIQUE INDEX `email` (`email`) 
) 
COLLATE='hebrew_general_ci' 
ENGINE=InnoDB; 

我得到的錯誤試圖創建另一個:

CREATE TABLE `userfilescategories` (
`name` VARCHAR(255) NULL DEFAULT NULL, 
`user` VARCHAR(60) NOT NULL, 
`id` INT(11) NOT NULL AUTO_INCREMENT, 
`createdAt` DATETIME NULL DEFAULT NULL, 
`updatedAt` DATETIME NULL DEFAULT NULL, 
PRIMARY KEY (`id`), 
INDEX `user` (`user`), 
CONSTRAINT `UserFilesCategories_Users_FK` FOREIGN KEY (`user`) REFERENCES `user` (`id`) ON DELETE CASCADE 
) 
COLLATE='utf8_general_ci' 
ENGINE=InnoDB 
AUTO_INCREMENT=6; 

我得到的錯誤是:

SQL Error (1005): Can't create table 'ev.userfilescategries' (errno:150) 
Foreign key constraint is incorrectly formed 

從Show Engine InnoDB狀態我得到:

Error in foreign key constraint of table ev/userfilescategories: 
FOREIGN KEY (`user`) REFERENCES `user` (`id`) ON DELETE CASCADE 
) 
COLLATE='utf8_general_ci' 
ENGINE=InnoDB 
AUTO_INCREMENT=6: 
Cannot find an index in the referenced table where the referenced columns appear as the first columns, or column types in the table and the referenced table do not match for constraint. 
Note that the internal storage type of ENUM and SET changed in tables created with >= InnoDB-4.1.12, and such columns in old tables cannot be referenced by such columns in new tables. 

我錯過了什麼?外鍵如何形成不正確?

+0

您可以發佈您收到的錯誤消息嗎? –

+0

針對確切的錯誤消息運行'show engine innodb status'。您可能需要以root身份運行查詢。 –

+0

我加了我得到的錯誤 –

回答

1

創建具有相同COLLATE(utf8_general_ci)這兩個表

CREATE TABLE `user` (
`email` VARCHAR(255) NULL DEFAULT NULL, 
`username` VARCHAR(255) NULL DEFAULT NULL, 
`password` VARCHAR(255) NULL DEFAULT NULL, 
`website` INT(11) NULL DEFAULT NULL, 
`facebook_id` VARCHAR(255) NULL DEFAULT NULL, 
`google_id` VARCHAR(255) NULL DEFAULT NULL, 
`provider` VARCHAR(255) NULL DEFAULT NULL, 
`getUpdates` TINYINT(1) NOT NULL, 
`id` INT(11) NOT NULL AUTO_INCREMENT, 
`createdAt` DATETIME NULL DEFAULT NULL, 
`updatedAt` DATETIME NULL DEFAULT NULL, 
PRIMARY KEY (`id`), 
UNIQUE INDEX `email` (`email`) 
) 
COLLATE='utf8_general_ci' 
ENGINE=INNODB; 

CREATE TABLE `userfilescategories` (
`name` VARCHAR(255) NULL DEFAULT NULL, 
`user` INT(11) NOT NULL, 
`id` INT(11) NOT NULL AUTO_INCREMENT, 
`createdAt` DATETIME NULL DEFAULT NULL, 
`updatedAt` DATETIME NULL DEFAULT NULL, 
PRIMARY KEY (`id`), 
INDEX `user` (`user`), 
CONSTRAINT `UserFilesCategories_Users_FK` FOREIGN KEY (`user`) REFERENCES `user` (`id`) ON DELETE CASCADE 
) 
COLLATE='utf8_general_ci' 
ENGINE=INNODB 
AUTO_INCREMENT=6; 

N:B:外鍵和引用的密鑰對應的字段必須具有類似的數據類型。整數類型的大小和符號必須相同。字符串類型的長度不需要相同。對於非二進制(字符)字符串列,字符集和歸類必須相同。

Reference

+0

非常感謝,那份工作,不能相信我錯過了那個 –

+0

你是最受歡迎的。很高興知道這對你有用。順便說一句,任何人都可能會錯過。 :p – 1000111