gpt4 book ai didi

mysql错误: 1215 Cannot add foreign key constraint[ foreign keys same type, innodb]

转载 作者:行者123 更新时间:2023-11-30 22:22:04 39 4
gpt4 key购买 nike

我需要一双新的眼睛来看看我到底做错了什么。

CREATE TABLE IF NOT EXISTS `spring_normalize`.`users` (
`username` VARCHAR(60) NOT NULL,
`password` VARCHAR(80) NULL,
`authority` VARCHAR(45) NULL,
`name` VARCHAR(100) NULL,
`enabled` TINYINT(1) NULL,
`email` VARCHAR(60) NULL,
PRIMARY KEY (`username`))
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `spring_normalize`.`offers` (
`id` INT NOT NULL AUTO_INCREMENT,
`text` VARCHAR(100) NULL,
`users_username` VARCHAR(60) NOT NULL,
PRIMARY KEY (`id`, `users_username`),
INDEX `fk_offers_users_idx` (`users_username` ASC),
CONSTRAINT `fk_offers_users`
FOREIGN KEY (`users_username`)
REFERENCES `spring_normalize`.`users` (`username`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

来自其他有同样问题的人:

  1. Db 是 InnoDB 吗?是的
  2. 所有表都是 InnoDB 吗?是的
  3. 引用表中是否存在唯一索引?是的
  4. 引用列和引用列是否完全属于同一类型?是的

质疑我做错了什么?提前致谢!

最佳答案

正如其他人在评论中所述,您的查询是正确的

您可以尝试一些事情:


首先选择您的数据库并将其从您的 2 create table

中删除
USE `spring_normalize`;


CREATE TABLE IF NOT EXISTS `users` (
`username` VARCHAR(60) NOT NULL,
`password` VARCHAR(80) NULL,
`authority` VARCHAR(45) NULL,
`name` VARCHAR(100) NULL,
`enabled` TINYINT(1) NULL,
`email` VARCHAR(60) NULL,
PRIMARY KEY (`username`))
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `offers` (
`id` INT NOT NULL AUTO_INCREMENT,
`text` VARCHAR(100) NULL,
`users_username` VARCHAR(60) NOT NULL,
PRIMARY KEY (`id`, `users_username`),
INDEX `fk_offers_users_idx` (`users_username` ASC),
CONSTRAINT `fk_offers_users`
FOREIGN KEY (`users_username`)
REFERENCES `users` (`username`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

尝试在创建表之前指定它,以防引擎会(奇怪地)尝试在 users 之前创建表 offers:

-- Do not check foreign key constraints
SET FOREIGN_KEY_CHECKS = 0;

最后,如果没有解决问题,请在收到错误后执行此操作,它将为您提供更多信息

SHOW ENGINE INNODB STATUS;

关于mysql错误: 1215 Cannot add foreign key constraint[ foreign keys same type, innodb],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36400166/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com