外键的MySql查询错误

时间:2014-10-20 12:35:35

标签: php mysql database magento foreign-keys

我在mysql数据库中插入这个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=96 DEFAULT CHARSET=utf8 COMMENT='Customer Address Entity';

但它显示错误消息

#1215 - 无法添加外键约束

如何解决此错误?

1 个答案:

答案 0 :(得分:0)

#1215 - Cannot add foreign key constraint

错误表示您的外键引用存在问题' customer_entity.entity_id'

您应该检查:

  • 表格' customer_entity'存在
  • ' customer_entity.entity_id'存在
  • ' customer_address_entity.parent_id'的类型和' customer_entity.entity_id'是完全相同的
  • 表格的字符集' customer_address_entity'和' customer_entity'是相同的

在任何情况下,请提供有关数据库架构的更多信息,或至少提供有关所涉及表的更多信息。