MYSQL Cant not create table errno: 150 Foreign key constraint is wrongly forms
Posted
技术标签:
【中文标题】MYSQL Cant not create table errno: 150 Foreign key constraint is wrongly forms【英文标题】:MYSQL Cant not create table errno: 150 Foreign key constraint is incorrectly formed 【发布时间】:2020-08-11 17:44:40 【问题描述】:使用 mysql workbench 创建表时外键出现问题。
当我用 mysql 工作台创建表时, 工作台给我这个错误:
在服务器中执行 SQL 脚本
ERROR: Error 1005: Can't create table kipit
.unlike_game_library
(errno: 150 "Foreign key constraint is misformed")
Executing SQL script in server
ERROR: Error 1005: Can't create table `kipit`.`unlike_game_library` (errno: 150 "Foreign key constraint is incorrectly formed")
SQL Code:
CREATE TABLE IF NOT EXISTS `kipit`.`unlike_game_library`
(
`id` BIGINT NOT NULL AUTO_INCREMENT,
`created_at` DATETIME NOT NULL DEFAULT now(),
`updated_at` DATETIME NULL DEFAULT now(),
`user_name` VARCHAR(100) NOT NULL,
`game_slug` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `user_name_UNIQUE` (`user_name` ASC),
CONSTRAINT `fk_rated_game_library_user`
FOREIGN KEY (`user_name`)
REFERENCES `kipit`.`user` (`name`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_rated_game_library_game1`
FOREIGN KEY (`game_slug`)
REFERENCES `kipit`.`game` (`slug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci
SQL script execution finished: statements: 11 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
这是我的查询:
-- MySQL Workbench Forward Engineering
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 =
'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema kipit
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `kipit`;
-- -----------------------------------------------------
-- Schema kipit
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `kipit` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE `kipit`;
-- -----------------------------------------------------
-- Table `kipit`.`user`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `kipit`.`user`;
CREATE TABLE IF NOT EXISTS `kipit`.`user`
(
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`role` VARCHAR(45) NOT NULL,
`name` VARCHAR(100) NOT NULL,
`email` VARCHAR(100) NOT NULL,
`picture` VARCHAR(100) NOT NULL,
`message` VARCHAR(255) NULL DEFAULT NULL,
`created_at` DATETIME NOT NULL DEFAULT now(),
`updated_at` DATETIME NULL DEFAULT now(),
PRIMARY KEY (`id`),
UNIQUE INDEX `name_UNIQUE` (`name` ASC)
)
ENGINE = InnoDB
AUTO_INCREMENT = 34
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `kipit`.`game`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `kipit`.`game`;
CREATE TABLE IF NOT EXISTS `kipit`.`game`
(
`id` BIGINT NOT NULL,
`name` VARCHAR(255) NOT NULL,
`slug` VARCHAR(255) NOT NULL,
`image` VARCHAR(255) NOT NULL,
`genre` VARCHAR(100) NULL,
`publisher` VARCHAR(255) NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `slug_UNIQUE` (`slug` ASC),
UNIQUE INDEX `name_UNIQUE` (`name` ASC),
UNIQUE INDEX `id_UNIQUE` (`id` ASC)
)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `kipit`.`unlike_game_library`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `kipit`.`unlike_game_library`;
CREATE TABLE IF NOT EXISTS `kipit`.`unlike_game_library`
(
`id` BIGINT NOT NULL AUTO_INCREMENT,
`created_at` DATETIME NOT NULL DEFAULT now(),
`updated_at` DATETIME NULL DEFAULT now(),
`user_name` VARCHAR(100) NOT NULL,
`game_slug` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `user_name_UNIQUE` (`user_name` ASC),
UNIQUE INDEX `game_slug_UNIQUE` (`game_slug` ASC),
CONSTRAINT `fk_rated_game_library_user`
FOREIGN KEY (`user_name`)
REFERENCES `kipit`.`user` (`name`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_rated_game_library_game1`
FOREIGN KEY (`game_slug`)
REFERENCES `kipit`.`game` (`slug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
SET SQL_MODE = @OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS = @OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS = @OLD_UNIQUE_CHECKS;
我检查了相同的类型和 unicode 设置以及唯一键,但它不起作用
这是我的 db fiddle 结果(完整查询):
https://www.db-fiddle.com/f/t2x2zvcVZZqxjsBsM7UXMF/3
+ INNO 状态
2020-04-28 11:43:42 0x1d68 Error in foreign key constraint of table `kipit`.`if`:
FOREIGN KEY (`user_name`)
REFERENCES `kipit`.`user` (`name`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_rated_game_library_game1`
FOREIGN KEY (`game_slug`)
REFERENCES `kipit`.`game` (`slug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
Please refer to https://mariadb.com/kb/en/library/foreign-keys/ for correct foreign key definition.
Create table `kipit`.`if` with foreign key constraint failed. Field type or character set for column 'user_name' does not mach referenced column 'name' near '
FOREIGN KEY (`user_name`)
REFERENCES `kipit`.`user` (`name`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_rated_game_library_game1`
FOREIGN KEY (`game_slug`)
REFERENCES `kipit`.`game` (`slug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci'.
【问题讨论】:
【参考方案1】:似乎“DEFAULT CHAR SET”和“COLLATION”不匹配。如何使所有正在使用的表都相同。
DROP TABLE IF EXISTS `kipit`.`user`;
CREATE TABLE IF NOT EXISTS `kipit`.`user`
(
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`role` VARCHAR(45) NOT NULL,
`name` VARCHAR(100) NOT NULL,
`email` VARCHAR(100) NOT NULL,
`picture` VARCHAR(100) NOT NULL,
`message` VARCHAR(255) NULL DEFAULT NULL,
`created_at` DATETIME NOT NULL DEFAULT now(),
`updated_at` DATETIME NULL DEFAULT now(),
PRIMARY KEY (`id`),
UNIQUE INDEX `name_UNIQUE` (`name` ASC)
)
ENGINE = InnoDB
AUTO_INCREMENT = 34
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
DROP TABLE IF EXISTS `kipit`.`game`;
CREATE TABLE IF NOT EXISTS `kipit`.`game`
(
`id` BIGINT NOT NULL,
`name` VARCHAR(255) NOT NULL,
`slug` VARCHAR(255) NOT NULL,
`image` VARCHAR(255) NOT NULL,
`genre` VARCHAR(100) NULL,
`publisher` VARCHAR(255) NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `slug_UNIQUE` (`slug` ASC) ,
UNIQUE INDEX `name_UNIQUE` (`name` ASC) ,
UNIQUE INDEX `id_UNIQUE` (`id` ASC)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
DROP TABLE IF EXISTS `kipit`.`unlike_game_library`;
CREATE TABLE IF NOT EXISTS `kipit`.`unlike_game_library`
(
`id` BIGINT NOT NULL AUTO_INCREMENT,
`created_at` DATETIME NOT NULL DEFAULT now(),
`updated_at` DATETIME NULL DEFAULT now(),
`user_name` VARCHAR(100) NOT NULL,
`game_slug` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `user_name_UNIQUE` (`user_name` ASC) ,
UNIQUE INDEX `game_slug_UNIQUE` (`game_slug` ASC) ,
CONSTRAINT `fk_rated_game_library_user`
FOREIGN KEY (`user_name`)
REFERENCES `kipit`.`user` (`name`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_rated_game_library_game1`
FOREIGN KEY (`game_slug`)
REFERENCES `kipit`.`game` (`slug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
【讨论】:
以上是关于MYSQL Cant not create table errno: 150 Foreign key constraint is wrongly forms的主要内容,如果未能解决你的问题,请参考以下文章
have only user privileges, so i cant use commands like create directory
Linux操作提示:“Cant open file for writing”或“operation not permitted”的解决办法
latin-1 codec cant encode characters in position 42-48: ordinal not in range256 下载文件时候报错