gpt4 book ai didi

MySQL Workbench 正向工程师错误 1215 : Cannot add foreign key constraint

转载 作者:可可西里 更新时间:2023-11-01 07:52:34 24 4
gpt4 key购买 nike

当我执行此脚本创建 2 个表时,其中 CUSTOMER 表中的 STORE 列引用 USERS 表中的 ID 列(两列均为 INT):

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,ALLOW_INVALID_DATES';

CREATE SCHEMA IF NOT EXISTS `part_finder` DEFAULT CHARACTER SET utf8 ;
USE `part_finder` ;

-- -----------------------------------------------------
-- Table `part_finder`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `part_finder`.`users` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`account` MEDIUMINT(7) UNSIGNED NOT NULL COMMENT 'Account organisation user belongs to',
`name` VARCHAR(32) NULL DEFAULT NULL,
`passenc` VARCHAR(32) NULL DEFAULT NULL,
`email` VARCHAR(55) NULL DEFAULT NULL,
`rank` DECIMAL(1,0) NULL DEFAULT '0',
`ip_reg` VARCHAR(15) NULL DEFAULT NULL,
`ip_visit` VARCHAR(15) NULL DEFAULT NULL,
`dtreg` INT(11) NOT NULL,
`dtvisit` INT(11) NOT NULL,
`visits` SMALLINT(5) UNSIGNED NULL DEFAULT '0',
`pass` VARCHAR(25) NULL DEFAULT NULL,
`make_filter_on` TINYINT(1) NULL DEFAULT FALSE,
`brand_filter_on` TINYINT(1) NULL DEFAULT FALSE,
PRIMARY KEY (`id`))
ENGINE = InnoDB
AUTO_INCREMENT = 3;


-- -----------------------------------------------------
-- Table `part_finder`.`customer`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `part_finder`.`customer` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`store` INT(10) NOT NULL,
`name` VARCHAR(32) NULL,
`address` VARCHAR(45) NULL,
`address_2` VARCHAR(45) NULL,
`city` VARCHAR(15) NULL,
`state` TINYINT UNSIGNED NOT NULL,
`zip` CHAR(5) NULL,
`phone` VARCHAR(15) NULL,
`website` VARCHAR(45) NULL,
`email` VARCHAR(55) NULL,
PRIMARY KEY (`id`),
INDEX `fk_customer_users_idx` (`store` ASC),
CONSTRAINT `fk_customer_users`
FOREIGN KEY (`store`)
REFERENCES `part_finder`.`users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;


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

我收到这个错误:

ERROR: Error 1215: Cannot add foreign key constraint

-- -----------------------------------------------------
-- Table `part_finder`.`customer`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `part_finder`.`customer` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`store` INT(10) NOT NULL,
`name` VARCHAR(32) NULL,
`address` VARCHAR(45) NULL,
`address_2` VARCHAR(45) NULL,
`city` VARCHAR(15) NULL,
`state` TINYINT UNSIGNED NOT NULL,
`zip` CHAR(5) NULL,
`phone` VARCHAR(15) NULL,
`website` VARCHAR(45) NULL,
`email` VARCHAR(55) NULL,
PRIMARY KEY (`id`),
INDEX `fk_customer_users_idx` (`store` ASC),
CONSTRAINT `fk_customer_users`
FOREIGN KEY (`store`)
REFERENCES `part_finder`.`users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB

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

Fetching back view definitions in final form.
Nothing to fetch

我使用的是 InnoDB,我已经检查了我的外键列的数据类型,并确保我使用的是引用表中的主键。

有什么想法吗?

最佳答案

也许父表中的列是 INT UNSIGNED

它们在两个表中的数据类型必须完全相同。

Foreign Key Constraints

可能出现外键约束错误的原因:

  1. 您正在尝试引用目标表中不存在的键。确保它是另一个表上的键(它可以是主键或唯一键),

  2. 列的类型不一样(除了列在引用表上可以是 nullable)。

关于MySQL Workbench 正向工程师错误 1215 : Cannot add foreign key constraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22904930/

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