【问题标题】:#1022 Duplicate foreign key - MySQL bug#1022 重复的外键 - MySQL 错误
【发布时间】:2018-06-20 16:01:46
【问题描述】:

我在将 FOREIGN KEY 添加到表 catalog_produt_flat_8 - Magento 1.9 时遇到问题

当我尝试重新索引时

Product Flat Data index process unknown error:
exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table '#sql-4c36_d67'

我尝试在 MySQL 中添加

ALTER TABLE catalog_product_flat_8
ADD CONSTRAINT FK_CAT_PRD_FLAT_8_ENTT_ID_CAT_PRD_ENTT_ENTT_ID FOREIGN KEY (entity_id) REFERENCES catalog_product_entity (entity_id) ON DELETE CASCADE ON UPDATE CASCADE;

还是

1022 Can't write; duplicate key in table '#sql-4c36_1099

我签入 information_schema

SELECT * FROM information_schema.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = "magento_db" AND CONSTRAINT_NAME LIKE "FK_CAT_PRD_FLAT_8_ENTT_ID_CAT_PRD_ENTT_ENTT_ID" 

但有 0 条记录

select COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_COLUMN_NAME, 
REFERENCED_TABLE_NAME
from information_schema.KEY_COLUMN_USAGE
where TABLE_NAME = 'catalog_product_flat_8';

我只有 entity_id 的主键

所以,我检查一下:

select *
from information_schema.table_constraints
where constraint_schema = 'magento_db'

entity_id (PRIMARY) 中只有 1 条记录为“flat_8”,

我在 information_schema 的每个表中搜索“FK_CAT_PRD_FLAT_8_ENTT_ID_CAT_PRD_ENTT_ENTT_ID”

结果:

`information_schema`.`INNODB_SYS_FOREIGN`

| ID                                                    |FOR_NAME                   |REF_NAME                           | N_COLS | TYPE |
| magento_db/FK_CAT_PRD_FLAT_8_ENTT_ID_CAT_PRD_ENTT_E... |magento_db/#sql-6756_19e0f | magento_db/catalog_product_entity | 1      | 5    |






`information_schema`.`INNODB_SYS_FOREIGN_COLS`

| ID                                                    | FOR_COL_NAME| REF_COL_NAME | POS |
| magento_db/FK_CAT_PRD_FLAT_8_ENTT_ID_CAT_PRD_ENTT_E...    | entity_id   | entity_id    |  0  |

我不知道,请帮忙

【问题讨论】:

    标签: mysql foreign-keys magento-1.9 on-duplicate-key


    【解决方案1】:

    我现在也有同样的问题。键在 INNODB_SYS_FOREIGN_COLS 中,但在 TABLE_CONSTRAINTS 中缺失。出于这个原因,显然不能更改表以删除密钥。

    我想出的唯一解决方案是转储、删除和导入数据库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-06
      • 1970-01-01
      • 1970-01-01
      • 2019-09-08
      • 2015-09-28
      • 2014-08-13
      • 2016-03-22
      相关资源
      最近更新 更多