#1022外键重复-MySQL错误

时间:2018-06-20 16:01:46

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

我在将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  |

我不知道,请帮助

1 个答案:

答案 0 :(得分:0)

我屈膝有同样的问题。密钥在INNODB_SYS_FOREIGN_COLS中,但在TABLE_CONSTRAINTS中丢失。因此,显然不能更改表以删除键。

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