不懂就问,在 mysql 中关于 associative entity(Credentials)报错应该怎么改?

2021-04-15 13:49:06 +08:00
 Lzjss
报错:Failed to add the foreign key constraint. Missing index for constraint 'FK_CREDENTIALS_USER' in the referenced table 'User'

具体代码:
DROP TABLE IF EXISTS `PharmacyManagementSystemDB`.`User` ;

CREATE TABLE IF NOT EXISTS `PharmacyManagementSystemDB`.`User` (
`user_id` TINYINT(9) NOT NULL AUTO_INCREMENT,
`email` VARCHAR(45) NOT NULL,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`full_name` VARCHAR(100) GENERATED ALWAYS AS (CONCAT(first_name," ",last_name)),
`phone_num` INT NOT NULL,
PRIMARY KEY (`user_id`, `email`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `PharmacyManagementSystemDB`.`Account`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PharmacyManagementSystemDB`.`Account` ;

CREATE TABLE IF NOT EXISTS `PharmacyManagementSystemDB`.`Account` (
`account_id` TINYINT NOT NULL AUTO_INCREMENT,
`created` TIMESTAMP NOT NULL,
`expires` TIMESTAMP NOT NULL,
`user_id` TINYINT NOT NULL,
`region` TINYINT NOT NULL,
`role` TINYINT NOT NULL,
PRIMARY KEY (`account_id`),
INDEX `FK_PK_USER_ACCOUNT_idx` (`user_id` ASC) VISIBLE,
INDEX `FK_PK_REGION_ACCOUNT_idx` (`region` ASC) VISIBLE,
INDEX `FK_PK_ROLES_ACCOUNT_idx` (`role` ASC) VISIBLE,
CONSTRAINT `FK_PK_USER_ACCOUNT`
FOREIGN KEY (`user_id`)
REFERENCES `PharmacyManagementSystemDB`.`User` (`user_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FK_PK_REGION_ACCOUNT`
FOREIGN KEY (`region`)
REFERENCES `PharmacyManagementSystemDB`.`Region` (`region_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FK_PK_ROLES_ACCOUNT`
FOREIGN KEY (`role`)
REFERENCES `PharmacyManagementSystemDB`.`Roles` (`Roles_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `PharmacyManagementSystemDB`.`Credentials`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PharmacyManagementSystemDB`.`Credentials` ;

CREATE TABLE IF NOT EXISTS `PharmacyManagementSystemDB`.`Credentials` (
`credentials_id` TINYINT NOT NULL,
`email` VARCHAR(45) NOT NULL,
`passwork` VARCHAR(45) NOT NULL,
`account_id` TINYINT NOT NULL,
PRIMARY KEY (`credentials_id`, `email`, `account_id`),
INDEX `FK_CREDENTIALS_USER_idx` (`email` ASC) VISIBLE,
INDEX `FK_CREDENTIALS_ACCTOUNT_idx` (`account_id` ASC) VISIBLE,
CONSTRAINT `FK_CREDENTIALS_USER`
FOREIGN KEY (`email`)
REFERENCES `PharmacyManagementSystemDB`.`User` (`email`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `FK_CREDENTIALS_ACCTOUNT`
FOREIGN KEY (`account_id`)
REFERENCES `PharmacyManagementSystemDB`.`Account` (`user_id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
656 次点击
所在节点    程序员
1 条回复
Lzjss
2021-04-21 05:07:35 +08:00
解决了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/770833

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX