2016-01-25 39 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