错误1822:无法添加外键constaint。缺少约束索引

时间:2016-03-22 13:30:31

标签: mysql

我发现了一些关于错误的线索。但是所有的解决方案对我都不起作用。

我的系统已经有以下表格:

CREATE TABLE `bdo2_agencia` (
  `cod_uf` char(2) NOT NULL,
  `cod_agencia` char(9) NOT NULL,
  `nome` varchar(100) NOT NULL,
  PRIMARY KEY (`cod_uf`,`cod_agencia`),
  KEY `fk_agencia_2_uf_idx` (`cod_uf`),
  CONSTRAINT `fk_agencia_2_uf` FOREIGN KEY (`cod_uf`) REFERENCES `bdo2_uf` (`cod_uf`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE `bdo2_login` (
  `login` char(30) NOT NULL,
  `ativo` tinyint(1) DEFAULT '1' COMMENT 'informa se o login está ativo',
  PRIMARY KEY (`login`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

我试图在它们之间创建另一个表,定义一个关系N:M

CREATE TABLE IF NOT EXISTS `bdo2`.`bdo2_login_agencia` (
  `cod_uf` CHAR(2) NOT NULL,
  `cod_agencia` CHAR(9) NOT NULL,
  `login` CHAR(30) NOT NULL,
  PRIMARY KEY (`cod_uf`, `cod_agencia`, `login`),
  INDEX `fk_login_2_login_agencia_idx` (`login` ASC),
  INDEX `fk_agencia_2_login_agencia_idx` (`cod_uf` ASC, `cod_agencia` ASC),
  CONSTRAINT `fk_agencia_2_login_agencia`
  FOREIGN KEY (`cod_uf` , `cod_agencia`)
  REFERENCES `bdo2`.`bdo2_agencia` (`cod_uf` , `cod_agencia`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_login_2_login_agencia`
  FOREIGN KEY (`login`) REFERENCES `bdo2`.`bdo2_login` (`login`) ON DELETE NO ACTION ON UPDATE NO ACTION)
ENGINE = InnoDB;

但我收到以下错误:

Error Code: 1215. Cannot add foreign key constraint

使用命令

SHOW ENGINE innodb STATUS

我收到以下消息:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
2016-03-22 10:14:05 7fe09c49f700 Error in foreign key constraint of table bdo2/bdo2_login_agencia:

FOREIGN KEY (`cod_uf` , `cod_agencia`)
REFERENCES `bdo2`.`bdo2_agencia` (`cod_uf` , `cod_agencia`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_login_2_login_agencia`
FOREIGN KEY (`login`) REFERENCES `bdo2`.`bdo2_login` (`login`)
ON DELETE NO ACTION ON UPDATE NO ACTION)
ENGINE = InnoDB:

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.
See http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
for correct foreign key definition.

所以我创建了没有约束的表,我试图单独创建约束

ALTER TABLE `bdo2`.`bdo2_login_agencia` 
ADD INDEX `fk_agencia_2_login_agencia_idx` (`cod_uf` ASC, `cod_agencia` ASC),
ADD INDEX `fk_login_2_login_agencia_idx` (`login` ASC);
ALTER TABLE `bdo2`.`bdo2_login_agencia` 
ADD CONSTRAINT `fk_agencia_2_login_agencia`
FOREIGN KEY (`cod_uf` , `cod_agencia`)
REFERENCES `bdo2`.`bdo2_agencia` (`cod_uf` , `cod_agencia`)
ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_login_2_login_agencia`
FOREIGN KEY (`login`)
REFERENCES `bdo2`.`bdo2_login` (`login`)
ON DELETE NO ACTION ON UPDATE NO ACTION;

但收到以下错误:

ERROR 1822: Failed to add the foreign key constaint. Missing index for constraint 'fk_agencia_2_login_agencia' in the referenced table 'bdo2_agencia'

如果索引存在,为什么要返回此消息? 这将是版本的错误?我在Linux上测试了两台服务器(版本5.6.23和5.6.29),两者都出现了同样的问题。

1 个答案:

答案 0 :(得分:0)

问题出在COLLATION上。我发现bdo2_login和bdo2_agencia表是使用CHARSET = utf8,但没有意识到Workbench将新表作为latin1。这足以击中解决的CHARSET和COLLATION。

我在FK的另一个创建中遇到了同样的问题,问题是COLLATION只有列。该表的PK具有COLLATION utf8,该列创建为FK为latin1。击中COLLATION就足以解决问题了。