Chào các bác!
Hiện mình đang làm tạo database bằng mysql nhưng có một lỗi khóa ngoại chưa biết fix. Bảng tạo ban đồng thành công thế này:
CREATE TABLE `spring_security`.`users` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`email` VARCHAR(45) NOT NULL,
`password` VARCHAR(255) NULL,
`phone` VARCHAR(45) NULL,
`username` VARCHAR(45) NULL,
PRIMARY KEY (`id`));
CREATE TABLE `spring_security`.`roles` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL,
PRIMARY KEY (`id`));
CREATE TABLE `spring_security`.`user_roles` (
`user_id` BIGINT(20) NOT NULL,
`role_id` BIGINT(20) NOT NULL,
PRIMARY KEY (`user_id`,`role_id`),
KEY `user_role_key` (`role_id`),
CONSTRAINT `user_userrole` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
CONSTRAINT `role_userrole` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`)
);
Nhưng lúc tạo thêm bảng để thêm thông tin cho user như sau:
CREATE TABLE `spring_security`.`user_info` (
`email` VARCHAR(45) NOT NULL,
`username` VARCHAR(45) NULL,
`phone` VARCHAR(45) NULL,
`age` VARCHAR(45) NULL,
`gender` VARCHAR(45) NULL,
`address` VARCHAR(255) NULL,
`introduce` VARCHAR(255) NULL,
`avatar` VARCHAR(45) NULL,
PRIMARY KEY (`email`),
CONSTRAINT `email_emailinfo` FOREIGN KEY (`email`) REFERENCES `users` (`email`)
);
Thì bị lỗi thế này:
ERROR 1822: Failed to add the foreign key constraint. Missing index for constraint 'email_emailinfo' in the referenced table 'users'
Các bác cho hỏi lỗi trên fix thế nào vậy ?