2014-11-23 112 views
0

这些列只是int(10)。外表列是主键。为什么这个错误?Mysql,未能创建外键

引用表:

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 

回答

1

首先,你要确保你的表引擎InnoDB,不MyISAM ,因为后者不支持外键。如果是这样的话,你可以改变发动机:

ALTER TABLE table_name ENGINE = InnoDB; 

如果这不是问题,你可以通过运行检查特定的错误:

SHOW ENGINE INNODB STATUS\G 

,然后在寻找LATEST FOREIGN KEY ERROR部分。

祝你好运!

+0

我无法获得索引有所作为。上面引用的doc指出将会隐含地创建一个索引。 – Interlated 2014-11-23 23:01:40

+0

看起来有前途的外键('financial_account_id')参考'civicrm_financial_account'('id'): 无法解析接近表名: ('id') --------- – Interlated 2014-11-24 00:08:28

+0

貌似表是myisam - 我们暂时还没有用过。 – Interlated 2014-11-24 00:19:49