Magento从1.4.0.1升级到1.9.2.3:升级安装程序引发错误:无法添加外键约束

Magento从1.4.0.1升级到1.9.2.3:升级安装程序引发错误:无法添加外键约束,magento,upgrade,magento-1.4,customer,Magento,Upgrade,Magento 1.4,Customer,尝试将magento系统从1.4.0.1升级到最新的1.9.2.3 升级安装程序文件mysql4-upgrade-1.4.0.0.7-1.4.0.0.8.php 抛出错误: a:5:{i:0;s:199:"Error in file: "app/code/core/Mage/Customer/sql/ customer_setup/mysql4-upgrade-1.4.0.0.7-1.4.0.0.8.php" - SQLSTATE[HY000]: General error: 1215 Cann

尝试将magento系统从1.4.0.1升级到最新的1.9.2.3

升级安装程序文件
mysql4-upgrade-1.4.0.0.7-1.4.0.0.8.php

抛出错误:

a:5:{i:0;s:199:"Error in file: "app/code/core/Mage/Customer/sql/
customer_setup/mysql4-upgrade-1.4.0.0.7-1.4.0.0.8.php" - SQLSTATE[HY000]:
General error: 1215 Cannot add foreign key constraint";i:1;s:970:"
#0 app/code/core/Mage/Core/Model/Resource/Setup.php(644): Mage::exception('Mage_Core', 'Error in file: ...')
  • 如何解决此问题
  • 错误的原因是什么
导出原始数据库时,请确保将Check Foreign Key Constraints设置为Off

导入此数据库时,问题应该不再存在。

原因: 实际上,这意味着脚本未能创建外键约束,正如错误实际存在的那样,但这些失败的原因其实很简单,如果您尝试链接的两个字段的类型和长度不完全相同,mysql将不允许您创建FK

现在,从这个更新脚本的第31行,您确实可以找到一个表的创建,在字段
attribute\u id

$installer->run("
CREATE TABLE `{$installer->getTable('customer/form_attribute')}` (
  `form_code` char(32) NOT NULL,
  `attribute_id` smallint UNSIGNED NOT NULL,
  PRIMARY KEY(`form_code`, `attribute_id`),
  KEY `IDX_CUSTOMER_FORM_ATTRIBUTE_ATTRIBUTE` (`attribute_id`),
  CONSTRAINT `FK_CUSTOMER_FORM_ATTRIBUTE_ATTRIBUTE` FOREIGN KEY (`attribute_id`) REFERENCES `{$installer->getTable('eav_attribute')}` (`attribute_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Customer attributes/forms relations';
");
但是您也可以看到,字段
属性\u id
被创建为最小但没有长度

由于此链接指向表
eav_属性
,您现在可以尝试从要创建的虚拟表中比较字段类型和
smallint
类型的字段

CREATE TABLE `dummy_table` (`attribute_id` smallint UNSIGNED NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;
# you should now have the table named "dummy_table"

show fields from dummy_table where field = 'attribute_id';
# the Type should be "smallint(5) unsigned" and Null should be "No", if not, that is why the foreign key creation fail.
show fields from eav_attribute where field = 'attribute_id';
# the Type should be "smallint(5) unsigned" and Null should be "No"

drop table `dummy_table`;
# clean up of our testing table
决议: 因此,现在,如果字段
eav\u attribute.attribute\u id
不是
smallint(5)unsigned not null
,那么您可以安全地编辑
eav\u attribute.attribute\u id
,使其
smallint(5)unsigned not null

如果您在虚拟表中创建的字段不是
smallint(5)unsigned not null
,那么只需编辑文件
mysql4-upgrade-1.4.0.0.7-1.4.0.0.8.php的第34行即可正确创建该字段

  // `attribute_id` smallint UNSIGNED NOT NULL, -- old line
  `attribute_id` smallint(5) UNSIGNED NOT NULL, // new line, so the field have the right type

这可能是数据库引擎的问题。几个月前我也遇到过类似的问题,所以我将更新后的magento数据库表引擎与1.9数据库表引擎进行了比较,发现所有表都将MyISAM作为数据库引擎,所以我将其更改为1.9数据库,我的问题得到了解决

这是什么?这与安装/升级文件有关,我在谷歌搜索时看到了这个结果,但我不确定这是否能帮到你。你能试着找出该文件中到底有什么不起作用吗?如果你做了一个
die('ok')在创建customer/form_属性表之前,它是否有效?如果在创建表之后立即执行此操作,它是否仍然有效?还是你得到了错误?谢谢你的回答。我将检查更改为
attribute\u id smallint(5)UNSIGNED NOT NULL
,但仍然得到相同的错误
显示来自eav\u属性的字段,其中字段='attribute\u id'
显示了什么?表
customer\u form\u attribute
是否已经存在于数据库中?如果创建了表,那么,在CREATETABLE语句之后,应该还有其他东西会失败,对吗?你能指出是哪条线在做这件事吗?添加一个简单的
die('ok')
,并将其从php行移动到php行,实际上会告诉我们是什么指令导致该操作失败
  // `attribute_id` smallint UNSIGNED NOT NULL, -- old line
  `attribute_id` smallint(5) UNSIGNED NOT NULL, // new line, so the field have the right type