Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 错误1215(HY000):无法添加外键约束_Mysql_Foreign Keys - Fatal编程技术网

Mysql 错误1215(HY000):无法添加外键约束

Mysql 错误1215(HY000):无法添加外键约束,mysql,foreign-keys,Mysql,Foreign Keys,我有两个表,我想创建一个外键,但出现了错误1215。 这是桌子 CREATE TABLE `entities_def` ( `entityid` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Entity ID', `parentid` int(10) unsigned DEFAULT NULL COMMENT 'Parent Entity ID', `nick` varchar(255) NOT NULL COMMENT 'E

我有两个表,我想创建一个外键,但出现了错误1215。 这是桌子

 CREATE TABLE `entities_def` (
  `entityid` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Entity ID',
  `parentid` int(10) unsigned DEFAULT NULL COMMENT 'Parent Entity ID',
  `nick` varchar(255) NOT NULL COMMENT 'Entity Nickname',
  `esid` int(10) unsigned NOT NULL DEFAULT '1' COMMENT 'Entity State ID',
  `rdn` varchar(255) NOT NULL COMMENT 'Entity LDAP Relative Distinguished Name',
  `etype` enum('physical','legal','structural','external','access') CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'physical' COMMENT 'Entity Type',
  `ctime` timestamp NULL DEFAULT NULL COMMENT 'Time of Creation',
  `mtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Time of Last Modification',
  `cby` int(10) unsigned DEFAULT NULL COMMENT 'Created By',
  `mby` int(10) unsigned DEFAULT NULL COMMENT 'Last Modified By',
  `descr` text COMMENT 'Entity Description',
  PRIMARY KEY (`entityid`),
  KEY `i_parentid` (`parentid`),
  KEY `i_nick` (`nick`),
  KEY `i_esid` (`esid`),
  KEY `i_cby` (`cby`),
  KEY `i_mby` (`mby`),
  CONSTRAINT `entities_def_fk_cby` FOREIGN KEY (`cby`) REFERENCES `users` (`uid`) ON DELETE SET NULL ON UPDATE CASCADE,
  CONSTRAINT `entities_def_fk_esid` FOREIGN KEY (`esid`) REFERENCES `entities_states` (`esid`) ON UPDATE CASCADE,
  CONSTRAINT `entities_def_fk_mby` FOREIGN KEY (`mby`) REFERENCES `users` (`uid`) ON DELETE SET NULL ON UPDATE CASCADE,
  CONSTRAINT `entities_def_fk_parentid` FOREIGN KEY (`parentid`) REFERENCES `entities_def` (`entityid`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 COMMENT='Entities' 
第二个:

CREATE TABLE `userdomains` (
  `id` int(32) NOT NULL AUTO_INCREMENT,
  `accessuserid` int(10) NOT NULL,
  `domainname` char(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Domain Names'
我试图创建一个外键约束,如下所示:

alter table userdomains ADD CONSTRAINT userdomains_fk_accessuserid FOREIGN KEY (accessuserid) REFERENCES entities_def (entityid) ;
但错误不会消失

运行
后显示引擎INNODB状态\G
我得到

    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.

但看起来一切正常,我引用的是父级的主键,两列的类型和长度都相同。。。无法找出问题所在。

我遇到了这个问题,并使用了
desc TABLE
检查我的表。我发现我试图在长度相同的有符号整数和无符号整数(在我的例子中是bigint(20))之间创建FK关系。请记住,int在默认情况下是有符号的,而在显式情况下是无符号的

看起来您遇到了相同的问题:

userdomains.accessuserid
定义为
int(10)notnull,

entities\u def.entityid
定义为
int(10)unsigned NOT NULL自动增量注释“Entity ID”,

这种关系的唯一问题是,两个int(10)值的定义范围不同,因此不兼容


如果我是你,我会将它们都定义为未签名。标准行业最佳实践规定,自动递增主键应始终从零开始。

运行SHOW ENGINE INNODB STATUS\G并查看最新外键错误部分


有关此命令的更多信息,请访问。

运行SHOW ENGINE INNODB STATUS\G并查看最新外键错误部分。