I've been trying to follow a tutorial that I've came across, which is about spring security. In some place, I need to create 2 tables in my database which is user
and authorities
. While doing this, I am using this script that is suggested like in the tutorial. http://docs.spring.io/spring-security/site/docs/3.0.x/reference/appendix-schema.html
I've already a user
table in my db, so I just need to add autohirites
table. Since I'm using MySQL, I've changed that query like below:
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);
Also, here is my user table too:
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
;
When I try to run my first query which is going to create authorities table, I am getting ERROR 1215: Cannot add foreign key constraint error.
So far, I've been looked into these questions below, but none of them answered my problem + I think they are both the same questions: