简体   繁体   English

同一个表的MySQL外键失败,错误1005,错误150

[英]MySQL foreign key to the same table failed with error 1005, errno 150

mysql> ALTER TABLE category ADD CONSTRAINT category_parent_category_id FOREIGN KEY (parent) REFERENCES category(id);
ERROR 1005 (HY000): Can't create table 'sfnews.#sql-244_1' (errno: 150)

DDL as follows: DDL如下:

Create Table: CREATE TABLE `category` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `parent` bigint(20) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`),
  KEY `parent_idx` (`parent`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

Why is it wrong? 为什么这是错的?

Self reference should be possible. 应该可以自我参考。 It's because "parent" is unsigned and "id" is not. 这是因为“父”是无符号的而“id”不是。 Change the table definitions id column to 将表定义id列更改为

 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,

and it will work. 它会起作用。

The reference states about foreign keys: "The size and sign of integer types must be the same" 关于外键的引用说明:“整数类型的大小和符号必须相同”

Seems to be the same problem described here 似乎是这里描述的相同问题

If you check the status of the InnoDB engine ( SHOW ENGINE InnoDB STATUS ), you'll get a fuller explanation: 如果您检查InnoDB引擎的状态( SHOW ENGINE InnoDB STATUS ),您将获得更全面的解释:

LATEST FOREIGN KEY ERROR 最新的外国关键错误

[...] [...]

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. 在引用的表中找不到索引,其中引用的列显示为第一列,或者表中的列类型与引用的表不匹配约束。

Make id unsigned. 使id无符号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL错误1005错误150错误地形成了外键约束 - MySQL error 1005 errno 150 Foreign key constraint is incorrectly formed MySQL 创建表:错误1005 errno:150“外键约束形成错误” - MySQL create table: error 1005 errno: 150 “Foreign key constraint is incorrectly formed” MySQL外键错误1005 errno 150(表引擎,字段类型,长度和索引都可以) - MySQL Foreign Key Error 1005 errno 150 (table engine, fields types, lengths and indexations are ok) MySQL 外键错误 1005 errno 150 主键作为外键 - MySQL Foreign Key Error 1005 errno 150 primary key as foreign key 使用 mysql 工作台创建 CHAR 类型的外键时出错:错误 1005:无法创建表(错误号:150) - Error when creating foreign key of type CHAR with mysql workbench: Error 1005: Can't create table (errno: 150) MYSQL Error Code: 1005. Can't create table `db_xcruz`.`users` (errno: 150 "Foreign key constraint is wrongly forms") - MYSQL Error Code: 1005. Can't create table `db_xcruz`.`users` (errno: 150 "Foreign key constraint is incorrectly formed") 外键错误 - 错误 1005 (HY000) … 无法创建表 … `stored_on` (errno: 150) - Foreign Key Error - ERROR 1005 (HY000) … Can't create table … `stored_on` (errno: 150) MySQL错误#1005错误150 - MySQL error #1005 errno 150 外键问题:错误1005(HY000):无法创建表(错误号:150) - Foreign key issue:ERROR 1005 (HY000): Can't create table (errno: 150) 一般错误:1005 无法创建表(错误号:150“外键约束格式不正确”) - General error: 1005 Can't create table (errno: 150 "Foreign key constraint is incorrectly formed")
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM