【发布时间】:2013-12-03 14:57:15
【问题描述】:
我看过无数关于相同错误的帖子,通常使用相同的解决方案,但对我来说并非如此!
我正在使用 Magento,在保存客户地址时遇到外键错误。错误如下:
SQLSTATE[23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(
carltonpackaging.customer_address_entity,CONSTRAINTFK_CUSTOMER_ADDRESS_ENTITY_PARENT_ID_CUSTOMER_ENTITY_ENTITY_IDFOREIGN KEY(parent_id)参考customer_entity(entity_id) O)
我试图在customer_address_entity 中更新的数据包括parent_id 的值3。 customer_entity 表中有一条记录,entity_id 为 3。
两个表都在使用 InnoDB,customer_entity.entity_id 和 customer_address_entity.parent_id 都是 INT(11)。
这也不是自定义功能,这是 Magento 的核心功能——我不知道为什么!
编辑:表定义:
CREATE TABLE `customer_address_entity` (
`entity_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Entity Id',
`entity_type_id` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Entity Type Id',
`attribute_set_id` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Attribute Set Id',
`increment_id` varchar(50) DEFAULT NULL COMMENT 'Increment Id',
`parent_id` int(10) unsigned DEFAULT NULL COMMENT 'Parent Id',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Created At',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Updated At',
`is_active` smallint(5) unsigned NOT NULL DEFAULT '1' COMMENT 'Is Active',
PRIMARY KEY (`entity_id`),
KEY `IDX_CUSTOMER_ADDRESS_ENTITY_PARENT_ID` (`parent_id`),
CONSTRAINT `FK_CUSTOMER_ADDRESS_ENTITY_PARENT_ID_CUSTOMER_ENTITY_ENTITY_ID` FOREIGN KEY (`parent_id`) REFERENCES `customer_entity` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=874 DEFAULT CHARSET=utf8 COMMENT='Customer Address Entity'
CREATE TABLE `customer_entity` (
`entity_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Entity Id',
`entity_type_id` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Entity Type Id',
`attribute_set_id` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Attribute Set Id',
`website_id` smallint(5) unsigned DEFAULT NULL COMMENT 'Website Id',
`email` varchar(255) DEFAULT NULL COMMENT 'Email',
`group_id` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Group Id',
`increment_id` varchar(50) DEFAULT NULL COMMENT 'Increment Id',
`store_id` smallint(5) unsigned DEFAULT '0' COMMENT 'Store Id',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Created At',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Updated At',
`is_active` smallint(5) unsigned NOT NULL DEFAULT '1' COMMENT 'Is Active',
`disable_auto_group_change` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Disable automatic group change based on VAT ID',
PRIMARY KEY (`entity_id`),
KEY `IDX_CUSTOMER_ENTITY_STORE_ID` (`store_id`),
KEY `IDX_CUSTOMER_ENTITY_ENTITY_TYPE_ID` (`entity_type_id`),
KEY `IDX_CUSTOMER_ENTITY_EMAIL_WEBSITE_ID` (`email`,`website_id`),
KEY `IDX_CUSTOMER_ENTITY_WEBSITE_ID` (`website_id`),
CONSTRAINT `FK_CUSTOMER_ENTITY_STORE_ID_CORE_STORE_STORE_ID` FOREIGN KEY (`store_id`) REFERENCES `core_store` (`store_id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `FK_CUSTOMER_ENTITY_WEBSITE_ID_CORE_WEBSITE_WEBSITE_ID` FOREIGN KEY (`website_id`) REFERENCES `core_website` (`website_id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=875 DEFAULT CHARSET=utf8 COMMENT='Customer Entity'
编辑:尽管发送了 3 的 parent_id,但 MySQL 收到了 0 的 parent_id,导致错误。还没弄明白为什么会这样,等我弄明白了再更新!
【问题讨论】:
-
你能得到发送到服务器的实际SQL语句吗?
-
表定义和Sql语句。更好的是小提琴。
-
我无法轻松获取整个 SQL 语句,因为它使用的是 Magento 的 EAV 结构,而且我不知道如何挖掘语句!我可以获得表格定义,将编辑我原来的问题
-
那么,你终于找到了获取语句的方法了:)
-
我做到了 :) 从来没有想过使用 MySQL 的通用查询日志!嗬!谢谢:)
标签: mysql sql magento key constraints