Mysql,无法创建外键
列仅为int(10)。“外部表”列是主键。为什么会出现这种错误 参考表:Mysql,无法创建外键,mysql,triggers,Mysql,Triggers,列仅为int(10)。“外部表”列是主键。为什么会出现这种错误 参考表: mysql> describe civicrm_entity_financial_account; +----------------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra
mysql> describe civicrm_entity_financial_account;
+----------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| entity_table | varchar(64) | NO | | NULL | |
| entity_id | int(10) unsigned | NO | | NULL | |
| account_relationship | int(10) unsigned | NO | | NULL | |
| financial_account_id | int(10) unsigned | NO | | NULL | |
+----------------------+------------------+------+-----+---------+----------------+
参考表:
mysql> describe civicrm_financial_account;
+-----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| account_type_id | int(10) unsigned | NO | | NULL | |
+-----------------+------------------+------+-----+---------+----------------+
SQL命令:
ALTER TABLE `civicrm_entity_financial_account` ADD CONSTRAINT `FK_civicrm_entity_financial_account_financial_account_id` FOREIGN KEY (`financial_account_id`) REFERENCES `civicrm_financial_account` (`id`);
ERROR 1215 (HY000): Cannot add foreign key constraint
首先,要确保表的引擎是
InnoDB
,而不是MyISAM
,因为后者不支持外键。如果是这种情况,您可以更换发动机:
ALTER TABLE table_name ENGINE = InnoDB;
如果这不是问题所在,您可以通过运行以下命令检查特定错误:
SHOW ENGINE INNODB STATUS\G
然后查看最新外键错误
部分
祝你好运 我无法获得索引来产生任何差异。上面引用的文档表示将隐式创建索引。外键(
financial\u account\u id
)引用civicrm\u financial\u account
(id
):无法解析靠近以下位置的表名:(id
)-----------看起来这些表是myisam-我们已经有一段时间没有使用它了。修复方法是将所有文件更改为InnoDB mysqldump | sed的#myisam#InnoDB#g | mysql-u。。。非常感谢你的帮助。@Interlated:太好了!实际上,您可以更轻松地更改表引擎:altertable table\u name engine=InnoDB代码>