[英]MySQL error 1005 errno 150 Foreign key constraint is incorrectly formed
[英]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如下:
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
為什么這是錯的?
如果您檢查InnoDB引擎的狀態( SHOW ENGINE InnoDB STATUS
),您將獲得更全面的解釋:
最新的外國關鍵錯誤
[...]
在引用的表中找不到索引,其中引用的列顯示為第一列,或者表中的列類型與引用的表不匹配約束。
使id
無符號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.