【发布时间】:2017-07-04 13:36:08
【问题描述】:
我有一个奇怪的错误,当 FOREIGN_KEY_CHECKS = 0 和 2 个查询被执行(除了 FOREIGN_CHECK_CHECKS 之外,这两种情况都一样。
该错误导致错误 ERROR 1823 (HY000): Failed to add the foreign key constraint...(以下为完整错误)
如果我使用 SET FOREIGN_KEY_CHECKS=1 运行查询,它会按预期工作。 (这似乎是反话,因为关闭外键检查应该让事情过去,但不应该)
这是 mysql 中的错误还是我不明白?我找不到关于这个 mysql 错误代码的太多信息。
MYSQL 版本: 5.6.33
错误(最后一个查询):
mysql> SET FOREIGN_KEY_CHECKS=0;
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER TABLE phppos_people ADD INDEX phppos_people_ibfk_1 (image_id);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE phppos_customers ADD CONSTRAINT phppos_customers_ibfk_1 FOREIGN KEY person_id (person_id) REFERENCES phppos_people (person_id) ON UPDATE NO ACTION ON DELETE NO ACTION;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE phppos_employees ADD CONSTRAINT phppos_employees_ibfk_1 FOREIGN KEY person_id (person_id) REFERENCES phppos_people (person_id) ON UPDATE NO ACTION ON DELETE NO ACTION;
ERROR 1823 (HY000): Failed to add the foreign key constraint 'migrate/person_id' to system tables
成功:
mysql> SET FOREIGN_KEY_CHECKS=1;
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER TABLE phppos_people ADD INDEX phppos_people_ibfk_1 (image_id);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE phppos_customers ADD CONSTRAINT phppos_customers_ibfk_1 FOREIGN KEY person_id (person_id) REFERENCES phppos_people (person_id) ON UPDATE NO ACTION ON DELETE NO ACTION;
Query OK, 1 row affected (0.03 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE phppos_employees ADD CONSTRAINT phppos_employees_ibfk_1 FOREIGN KEY person_id (person_id) REFERENCES phppos_people (person_id) ON UPDATE NO ACTION ON DELETE NO ACTION;
Query OK, 1 row affected (0.04 sec)
Records: 1 Duplicates: 0 Warnings: 0
架构:
mysql> show create table phppos_people;
+---------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| phppos_people | CREATE TABLE `phppos_people` (
`first_name` varchar(255) CHARACTER SET ucs2 NOT NULL,
`last_name` varchar(255) CHARACTER SET ucs2 NOT NULL,
`phone_number` varchar(255) CHARACTER SET ucs2 NOT NULL,
`email` varchar(255) CHARACTER SET ucs2 NOT NULL,
`address_1` varchar(255) CHARACTER SET ucs2 NOT NULL,
`address_2` varchar(255) CHARACTER SET ucs2 NOT NULL,
`city` varchar(255) CHARACTER SET ucs2 NOT NULL,
`state` varchar(255) CHARACTER SET ucs2 NOT NULL,
`zip` varchar(255) CHARACTER SET ucs2 NOT NULL,
`country` varchar(255) CHARACTER SET ucs2 NOT NULL,
`comments` text CHARACTER SET ucs2 NOT NULL,
`image_id` int(11) DEFAULT NULL,
`person_id` int(11) NOT NULL,
PRIMARY KEY (`person_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+---------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show create table phppos_customers;
+------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| phppos_customers | CREATE TABLE `phppos_customers` (
`id` int(11) NOT NULL,
`person_id` int(11) NOT NULL,
`account_number` varchar(255) CHARACTER SET ucs2 DEFAULT NULL,
`override_default_tax` int(11) NOT NULL,
`company_name` varchar(255) CHARACTER SET ucs2 NOT NULL,
`balance` decimal(23,10) NOT NULL,
`credit_limit` decimal(23,10) DEFAULT NULL,
`points` decimal(23,10) NOT NULL,
`current_spend_for_points` decimal(23,10) NOT NULL,
`current_sales_for_discount` int(11) NOT NULL,
`taxable` int(11) NOT NULL,
`tax_certificate` varchar(255) CHARACTER SET ucs2 NOT NULL,
`cc_token` varchar(255) CHARACTER SET ucs2 DEFAULT NULL,
`cc_preview` varchar(255) CHARACTER SET ucs2 DEFAULT NULL,
`card_issuer` varchar(255) CHARACTER SET ucs2 DEFAULT NULL,
`tier_id` int(11) DEFAULT NULL,
`deleted` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
【问题讨论】:
标签: mysql