【发布时间】:2016-12-11 15:04:39
【问题描述】:
我一直在尝试遵循我遇到的关于弹簧安全性的教程。在某些地方,我需要在我的数据库中创建 2 个表,即 user 和 authorities。在执行此操作时,我正在使用本教程中建议的脚本。 http://docs.spring.io/spring-security/site/docs/3.0.x/reference/appendix-schema.html
我的数据库中已经有一个user 表,所以我只需要添加autohirites 表。由于我使用的是 MySQL,因此我已将查询更改如下:
create table authorities (
username varchar(70) not null,
authority varchar(50) not null,
CONSTRAINT fk_authorities_users foreign key(username) references user(userFirstName));
create unique index ix_auth_username on authorities (username,authority);
另外,这也是我的用户表:
CREATE TABLE `user` (
`userId` INT(11) NOT NULL AUTO_INCREMENT,
`userFirstName` VARCHAR(70) NOT NULL,
`userLastName` VARCHAR(70) NOT NULL,
`userEmail` VARCHAR(70) NOT NULL,
`userAddress` VARCHAR(500) NULL DEFAULT NULL,
`userPhoneNumber` INT(13) NULL DEFAULT NULL,
`isActive` BINARY(50) NULL DEFAULT '1\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0',
`userPassword` VARCHAR(50) NOT NULL,
`userConfirmPassword` VARCHAR(50) NOT NULL,
PRIMARY KEY (`userId`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=2
;
当我尝试运行将创建权限表的第一个查询时,我收到 ERROR 1215: Cannot add foreign key constraint 错误。
到目前为止,我一直在研究以下这些问题,但没有一个回答我的问题 + 我认为它们都是相同的问题:
【问题讨论】:
-
两个表都设置为 InnoDB 吗?
-
在权限表中设置用户名主键。
标签: mysql database spring foreign-keys