gpt4 book ai didi

从 mysql 工作台创建表时出现 MySQL 150 错误(外键选项与列中的非空值冲突,已修复)

转载 作者:行者123 更新时间:2023-11-29 03:42:01 26 4
gpt4 key购买 nike

因此,当我从 mysql workbench 正向设计一个模式时,我遇到了一个相当大的 150 错误。

我已确保所有 pk 和 fk 具有相同的类型。所有表都是innodb引擎。

也许我在这里遗漏了一些愚蠢的东西(如果遗漏了,我深表歉意),但非常感谢您的帮助。

这是因 150 错误而失败的脚本:

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

SHOW WARNINGS;
CREATE SCHEMA IF NOT EXISTS `bis` DEFAULT CHARACTER SET latin1 ;
SHOW WARNINGS;
USE `bis` ;

-- -----------------------------------------------------
-- Table `bis`.`entities`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `bis`.`entities` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `bis`.`entities` (
`entity_name` VARCHAR(64) NOT NULL ,
`entity_description` TEXT NULL DEFAULT NULL ,
`entity_email` TEXT NOT NULL ,
`entity_phone` VARCHAR(11) NULL DEFAULT NULL ,
`entity_website` VARCHAR(64) NULL DEFAULT NULL ,
PRIMARY KEY (`entity_name`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;

SHOW WARNINGS;

-- -----------------------------------------------------
-- Table `bis`.`users`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `bis`.`users` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `bis`.`users` (
`user_name` VARCHAR(64) NOT NULL ,
`user_entity` VARCHAR(64) NOT NULL ,
`user_email` TEXT NOT NULL ,
`user_password` CHAR(64) NOT NULL ,
`user_salt` CHAR(64) NOT NULL ,
`user_role` VARCHAR(45) NOT NULL ,
`user_realName` VARCHAR(64) NOT NULL ,
`user_surname` VARCHAR(64) NOT NULL ,
`user_IDnumber` VARCHAR(13) NOT NULL ,
`user_cellNumber` VARCHAR(11) NOT NULL ,
PRIMARY KEY (`user_name`) ,
INDEX `entity_id` (`user_entity` ASC) ,
CONSTRAINT `entity_id`
FOREIGN KEY (`user_entity` )
REFERENCES `bis`.`entities` (`entity_name` )
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;

SHOW WARNINGS;

-- -----------------------------------------------------
-- Table `bis`.`reports`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `bis`.`reports` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `bis`.`reports` (
`report_id` INT NOT NULL AUTO_INCREMENT ,
`user_name` VARCHAR(64) NOT NULL ,
`investigator_name` VARCHAR(64) NOT NULL ,
`report_entity` VARCHAR(64) NOT NULL ,
`report_finished` TINYINT(1) NULL DEFAULT false ,
`submit_date` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ,
`finished_date` TIMESTAMP NULL ,
`report_PDFFile` LONGBLOB NULL ,
PRIMARY KEY (`report_id`) ,
INDEX `user_id` (`user_name` ASC) ,
INDEX `investigator_id` (`investigator_name` ASC) ,
INDEX `entity_id` (`report_entity` ASC) ,
CONSTRAINT `user_id`
FOREIGN KEY (`user_name` )
REFERENCES `bis`.`users` (`user_name` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `investigator_id`
FOREIGN KEY (`investigator_name` )
REFERENCES `bis`.`users` (`user_name` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `entity_id`
FOREIGN KEY (`report_entity` )
REFERENCES `bis`.`entities` (`entity_name` )
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;

SHOW WARNINGS;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

然后是对应的错误:

Executing SQL script in server

ERROR: Error 1005: Can't create table 'bis.users' (errno: 150)

CREATE TABLE IF NOT EXISTS `bis`.`users` (
`user_name` VARCHAR(64) NOT NULL ,
`user_entity` VARCHAR(64) NOT NULL ,
`user_email` TEXT NOT NULL ,
`user_password` CHAR(64) NOT NULL ,
`user_salt` CHAR(64) NOT NULL ,
`user_role` VARCHAR(45) NOT NULL ,
`user_realName` VARCHAR(64) NOT NULL ,
`user_surname` VARCHAR(64) NOT NULL ,
`user_IDnumber` VARCHAR(13) NOT NULL ,
`user_cellNumber` VARCHAR(11) NOT NULL ,
PRIMARY KEY (`user_name`) ,
INDEX `entity_id` (`user_entity` ASC) ,
CONSTRAINT `entity_id`
FOREIGN KEY (`user_entity` )
REFERENCES `bis`.`entities` (`entity_name` )
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1

SQL script execution finished: statements: 13 succeeded, 1 failed

最佳答案

users 表创建了一个名为 entity_id 的索引,同时将 fk 约束命名为 entity_id

我相信您不能拥有同名的索引和约束。

试试这个:

INDEX entity_id (user_entity ASC) ,
CONSTRAINT fk_user_entity -- *** this is the change ***
FOREIGN KEY (user_entity )
REFERENCES bis.entities (entity_name )
ON DELETE SET NULL
ON UPDATE CASCADE

关于从 mysql 工作台创建表时出现 MySQL 150 错误(外键选项与列中的非空值冲突,已修复),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12651619/

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