MYSQL 5.7错误1215外键+全文键

时间:2015-11-11 12:15:04

标签: mysql foreign-keys full-text-search mysql-5.7

我有以下问题。

我有dump创建的sql文件,其中包含代码:

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

/*SOME MORE SCRIPTS*/

DROP TABLE IF EXISTS `avalues`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;

CREATE TABLE `avalues` (
  `AVALUES_ID` INT(11) NOT NULL AUTO_INCREMENT,
  `AVALUES_MVALUES_ID` INT(11) NOT NULL DEFAULT '0',
  `AVALUES_OBJECT_ID` INT(11) NOT NULL DEFAULT '0',
  `AVALUES_VALUE` LONGTEXT,
  `AVALUES_RANG` INT(11) NOT NULL DEFAULT '0',
  `AVALUES_VALUE_NUM` DOUBLE DEFAULT NULL,
  `AVALUES_VALUE_DATE` DATETIME DEFAULT NULL,
  `AVALUES_VALUE_TIME` TIME DEFAULT NULL,
  `AVALUES_TOBJECT_ID` INT(11) DEFAULT NULL,
  PRIMARY KEY (`AVALUES_ID`),
  UNIQUE KEY `UK_AVALUES_MVALUES_OBJECT_RANG` (`AVALUES_MVALUES_ID`,`AVALUES_OBJECT_ID`,`AVALUES_RANG`),
  KEY `I_AVALUES_MVALUES` (`AVALUES_MVALUES_ID`),
  KEY `I_AVALUES_OBJECT` (`AVALUES_OBJECT_ID`),
  KEY `I_AVALUES_MVALUES_NUM` (`AVALUES_MVALUES_ID`,`AVALUES_VALUE_NUM`),
  KEY `I_AVALUES_MVALUES_DATE` (`AVALUES_MVALUES_ID`,`AVALUES_VALUE_DATE`),
  KEY `i_avalues_object_mvalues` (`AVALUES_OBJECT_ID`,`AVALUES_MVALUES_ID`),
  KEY `I_AVALUES_VALUE` (`AVALUES_VALUE`(255)),
  KEY `I_AVALUES_MVALUES_VALUE` (`AVALUES_MVALUES_ID`,`AVALUES_VALUE`(255)),
  KEY `i_avalues_tobject_value` (`AVALUES_TOBJECT_ID`,`AVALUES_VALUE`(255))
  , KEY `i_avalues_object_mvalues_value` (`AVALUES_OBJECT_ID`,`AVALUES_MVALUES_ID`,`AVALUES_VALUE`(255))
   , CONSTRAINT `FK_AVALUES_MVALUES_ID` FOREIGN KEY (`AVALUES_MVALUES_ID`) REFERENCES `mask_values` (`MVALUES_ID`) ON DELETE CASCADE
   , CONSTRAINT `FK_AVALUES_OBJECT_ID` FOREIGN KEY (`AVALUES_OBJECT_ID`) REFERENCES `object` (`OBJECT_ID`) ON DELETE CASCADE
   , FULLTEXT KEY `I_AVALUES_VALUE_FULLTEXT` (`AVALUES_VALUE`)
) ENGINE=INNODB AUTO_INCREMENT=3463676 DEFAULT CHARSET=utf8;

/*SOME MORE SCRIPTS*/

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

我正在使用 InnoDB 引擎。

之前我有mysql版本 5.6.5-m8-log 之前一切正常,所有脚本都已经使用mysql正确处理。

今天我将mysql更新为版本 5.7.9-log ,当我想要运行这些脚本时,不会创建表格avalues。

我收到以下错误: 错误代码:1215 无法添加外键约束

当我从脚本行中取出

FULLTEXT KEY `I_AVALUES_VALUE_FULLTEXT` (`AVALUES_VALUE`)

OR 两行

, CONSTRAINT `FK_AVALUES_MVALUES_ID` FOREIGN KEY (`AVALUES_MVALUES_ID`) REFERENCES `mask_values` (`MVALUES_ID`) ON DELETE CASCADE
, CONSTRAINT `FK_AVALUES_OBJECT_ID` FOREIGN KEY (`AVALUES_OBJECT_ID`) REFERENCES `object` (`OBJECT_ID`) ON DELETE CASCADE

表已正确创建。但似乎如果所有三条线都在一起,则会引起上述错误。

我已经读过,在mysql 5.6之前,这两个键(外来和全文)不能在一个表中。但我使用的是mysql 5.7.9版。我试图谷歌问题,但我找不到适合我的解决方案。

感谢您的回答

1 个答案:

答案 0 :(得分:1)

这是一个错误:Bug #78955: Import Dump with InnoDB Fulltext Index and Constraint fails

尝试以下更改,首先创建引用的表:

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

DROP TABLE IF EXISTS `avalues`;
DROP TABLE IF EXISTS `mask_values`;
DROP TABLE IF EXISTS `object`;

CREATE TABLE `mask_values` (
  `MVALUES_ID` INT(11) AUTO_INCREMENT PRIMARY KEY
) ENGINE=INNODB DEFAULT CHARSET=utf8;

CREATE TABLE `object` (
  `OBJECT_ID` INT(11) AUTO_INCREMENT PRIMARY KEY
) ENGINE=INNODB DEFAULT CHARSET=utf8;

CREATE TABLE `avalues` (
  `AVALUES_ID` INT(11) NOT NULL AUTO_INCREMENT,
  `AVALUES_MVALUES_ID` INT(11) NOT NULL DEFAULT '0',
  `AVALUES_OBJECT_ID` INT(11) NOT NULL DEFAULT '0',
  `AVALUES_VALUE` LONGTEXT,
  `AVALUES_RANG` INT(11) NOT NULL DEFAULT '0',
  `AVALUES_VALUE_NUM` DOUBLE DEFAULT NULL,
  `AVALUES_VALUE_DATE` DATETIME DEFAULT NULL,
  `AVALUES_VALUE_TIME` TIME DEFAULT NULL,
  `AVALUES_TOBJECT_ID` INT(11) DEFAULT NULL,
  PRIMARY KEY (`AVALUES_ID`),
  UNIQUE KEY `UK_AVALUES_MVALUES_OBJECT_RANG` (`AVALUES_MVALUES_ID`,`AVALUES_OBJECT_ID`,`AVALUES_RANG`),
  KEY `I_AVALUES_MVALUES` (`AVALUES_MVALUES_ID`),
  KEY `I_AVALUES_OBJECT` (`AVALUES_OBJECT_ID`),
  KEY `I_AVALUES_MVALUES_NUM` (`AVALUES_MVALUES_ID`,`AVALUES_VALUE_NUM`),
  KEY `I_AVALUES_MVALUES_DATE` (`AVALUES_MVALUES_ID`,`AVALUES_VALUE_DATE`),
  KEY `i_avalues_object_mvalues` (`AVALUES_OBJECT_ID`,`AVALUES_MVALUES_ID`),
  KEY `I_AVALUES_VALUE` (`AVALUES_VALUE`(255)),
  KEY `I_AVALUES_MVALUES_VALUE` (`AVALUES_MVALUES_ID`,`AVALUES_VALUE`(255)),
  KEY `i_avalues_tobject_value` (`AVALUES_TOBJECT_ID`,`AVALUES_VALUE`(255)),
  KEY `i_avalues_object_mvalues_value` (`AVALUES_OBJECT_ID`,`AVALUES_MVALUES_ID`,`AVALUES_VALUE`(255)),
  CONSTRAINT `FK_AVALUES_MVALUES_ID` FOREIGN KEY (`AVALUES_MVALUES_ID`) REFERENCES `mask_values` (`MVALUES_ID`) ON DELETE CASCADE,
  CONSTRAINT `FK_AVALUES_OBJECT_ID` FOREIGN KEY (`AVALUES_OBJECT_ID`) REFERENCES `object` (`OBJECT_ID`) ON DELETE CASCADE,
  FULLTEXT KEY `I_AVALUES_VALUE_FULLTEXT` (`AVALUES_VALUE`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

/*
CREATE TABLE `mask_values` (
  `MVALUES_ID` INT(11) AUTO_INCREMENT PRIMARY KEY
) ENGINE=INNODB DEFAULT CHARSET=utf8;

CREATE TABLE `object` (
  `OBJECT_ID` INT(11) AUTO_INCREMENT PRIMARY KEY
) ENGINE=INNODB DEFAULT CHARSET=utf8;
*/

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

mysql> /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
Query OK, 0 rows affected (0.00 sec)

mysql> /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
Query OK, 0 rows affected (0.00 sec)

mysql> /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
Query OK, 0 rows affected (0.00 sec)

mysql> /*!40101 SET NAMES utf8 */;
Query OK, 0 rows affected (0.00 sec)

mysql> /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
Query OK, 0 rows affected (0.00 sec)

mysql> /*!40103 SET TIME_ZONE='+00:00' */;
Query OK, 0 rows affected (0.00 sec)

mysql> /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
Query OK, 0 rows affected (0.00 sec)

mysql> /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
Query OK, 0 rows affected (0.00 sec)

mysql> /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE IF EXISTS `avalues`;
Query OK, 0 rows affected (0.01 sec)

mysql> DROP TABLE IF EXISTS `mask_values`;
Query OK, 0 rows affected (0.01 sec)

mysql> DROP TABLE IF EXISTS `object`;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE `mask_values` (
    ->   `MVALUES_ID` INT(11) AUTO_INCREMENT PRIMARY KEY
    -> ) ENGINE=INNODB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE `object` (
    ->   `OBJECT_ID` INT(11) AUTO_INCREMENT PRIMARY KEY
    -> ) ENGINE=INNODB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE `avalues` (
    ->   `AVALUES_ID` INT(11) NOT NULL AUTO_INCREMENT,
    ->   `AVALUES_MVALUES_ID` INT(11) NOT NULL DEFAULT '0',
    ->   `AVALUES_OBJECT_ID` INT(11) NOT NULL DEFAULT '0',
    ->   `AVALUES_VALUE` LONGTEXT,
    ->   `AVALUES_RANG` INT(11) NOT NULL DEFAULT '0',
    ->   `AVALUES_VALUE_NUM` DOUBLE DEFAULT NULL,
    ->   `AVALUES_VALUE_DATE` DATETIME DEFAULT NULL,
    ->   `AVALUES_VALUE_TIME` TIME DEFAULT NULL,
    ->   `AVALUES_TOBJECT_ID` INT(11) DEFAULT NULL,
    ->   PRIMARY KEY (`AVALUES_ID`),
    ->   UNIQUE KEY `UK_AVALUES_MVALUES_OBJECT_RANG` (`AVALUES_MVALUES_ID`,`AVALUES_OBJECT_ID`,`AVALUES_RANG`),
    ->   KEY `I_AVALUES_MVALUES` (`AVALUES_MVALUES_ID`),
    ->   KEY `I_AVALUES_OBJECT` (`AVALUES_OBJECT_ID`),
    ->   KEY `I_AVALUES_MVALUES_NUM` (`AVALUES_MVALUES_ID`,`AVALUES_VALUE_NUM`),
    ->   KEY `I_AVALUES_MVALUES_DATE` (`AVALUES_MVALUES_ID`,`AVALUES_VALUE_DATE`),
    ->   KEY `i_avalues_object_mvalues` (`AVALUES_OBJECT_ID`,`AVALUES_MVALUES_ID`),
    ->   KEY `I_AVALUES_VALUE` (`AVALUES_VALUE`(255)),
    ->   KEY `I_AVALUES_MVALUES_VALUE` (`AVALUES_MVALUES_ID`,`AVALUES_VALUE`(255)),
    ->   KEY `i_avalues_tobject_value` (`AVALUES_TOBJECT_ID`,`AVALUES_VALUE`(255)),
    ->   KEY `i_avalues_object_mvalues_value` (`AVALUES_OBJECT_ID`,`AVALUES_MVALUES_ID`,`AVALUES_VALUE`(255)),
    ->   CONSTRAINT `FK_AVALUES_MVALUES_ID` FOREIGN KEY (`AVALUES_MVALUES_ID`) REFERENCES `mask_values` (`MVALUES_ID`) ON DELETE CASCADE,
    ->   CONSTRAINT `FK_AVALUES_OBJECT_ID` FOREIGN KEY (`AVALUES_OBJECT_ID`) REFERENCES `object` (`OBJECT_ID`) ON DELETE CASCADE,
    ->   FULLTEXT KEY `I_AVALUES_VALUE_FULLTEXT` (`AVALUES_VALUE`)
    -> ) ENGINE=INNODB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.02 sec)

mysql> /*
   /*> CREATE TABLE `mask_values` (
   /*>   `MVALUES_ID` INT(11) AUTO_INCREMENT PRIMARY KEY
   /*> ) ENGINE=INNODB DEFAULT CHARSET=utf8;
   /*> 
   /*> CREATE TABLE `object` (
   /*>   `OBJECT_ID` INT(11) AUTO_INCREMENT PRIMARY KEY
   /*> ) ENGINE=INNODB DEFAULT CHARSET=utf8;
   /*> */

mysql> /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
Query OK, 0 rows affected (0.00 sec)

mysql> /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
Query OK, 0 rows affected (0.00 sec)

mysql> /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
Query OK, 0 rows affected (0.00 sec)

mysql> /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
Query OK, 0 rows affected (0.00 sec)

mysql> /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Query OK, 0 rows affected (0.00 sec)

mysql> /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
Query OK, 0 rows affected (0.00 sec)

我正在寻找行为的原因。

<强>更新

在创建表格后尝试SHOW ENGINE INNODB STATUS(请参阅13.7.5.15 SHOW ENGINE Syntax)时,您可以阅读以下内容:

...
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2000-01-01 00:00:01 0x901ffb40 Error in foreign key constraint of table test/avalues:
 FOREIGN KEY (`AVALUES_MVALUES_ID`) REFERENCES `mask_values` (`MVALUES_ID`) ON DELETE CASCADE,
  CONSTRAINT `FK_AVALUES_OBJECT_ID` FOREIGN KEY (`AVALUES_OBJECT_ID`) REFERENCES `object` (`OBJECT_ID`) ON DELETE CASCADE,
  FULLTEXT KEY `I_AVALUES_VALUE_FULLTEXT` (`AVALUES_VALUE`)
) ENGINE=INNODB DEFAULT CHARSET=utf8:
Cannot resolve table name close to:
 (`MVALUES_ID`) ON DELETE CASCADE,
  CONSTRAINT `FK_AVALUES_OBJECT_ID` FOREIGN KEY (`AVALUES_OBJECT_ID`) REFERENCES `object` (`OBJECT_ID`) ON DELETE CASCADE,
  FULLTEXT KEY `I_AVALUES_VALUE_FULLTEXT` (`AVALUES_VALUE`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
...