MySQL 中的外键添加失败,错误代码 1005,编号 150
因此,我尝试向我的一个表中添加一个新的外键:
ALTER TABLE `UserTransactions`.`ExpenseBackTransactions`
ADD CONSTRAINT `FK_EBTx_CustomAccountID`
FOREIGN KEY (`CustomAccountID` )
REFERENCES `UserTransactions`.`CustomAccounts` (`CustomAccountID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
ADD INDEX `FK_EBTx_CustomAccountID` (`CustomAccountID` ASC) ;
并且我不断收到以下错误:
Error Code: 1005
Can't create table './UserTransactions/#sql-187a_29.frm' (errno: 150)
我过去对此表和其他表进行了相当多的更改,这是第一个我曾经遇到过这个问题。有什么想法造成它吗?
更新
我的SHOW INNODB STATUS
错误:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
110525 15:56:36 Error in foreign key constraint of table UserTransactions/#sql-187a_2c:
FOREIGN KEY (`CustomAccountID` )
REFERENCES `UserTransactions`.`CustomAccounts` (`CustomAccountID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION
, ADD INDEX `FK_EBTx_CustomAccountID` (`CustomAccountID` ASC):
Cannot resolve table name close to:
(`CustomAccountID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION
, ADD INDEX `FK_EBTx_CustomAccountID` (`CustomAccountID` ASC)
So I'm attempting to add a new foreign key to one of my tables as such:
ALTER TABLE `UserTransactions`.`ExpenseBackTransactions`
ADD CONSTRAINT `FK_EBTx_CustomAccountID`
FOREIGN KEY (`CustomAccountID` )
REFERENCES `UserTransactions`.`CustomAccounts` (`CustomAccountID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
ADD INDEX `FK_EBTx_CustomAccountID` (`CustomAccountID` ASC) ;
and I keep getting the following error:
Error Code: 1005
Can't create table './UserTransactions/#sql-187a_29.frm' (errno: 150)
I've done quite a bit of changes in the past to this and other tables, and this is the first time I've run into this issue. Any ideas what is causing it?
UPDATE
My SHOW INNODB STATUS
error:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
110525 15:56:36 Error in foreign key constraint of table UserTransactions/#sql-187a_2c:
FOREIGN KEY (`CustomAccountID` )
REFERENCES `UserTransactions`.`CustomAccounts` (`CustomAccountID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION
, ADD INDEX `FK_EBTx_CustomAccountID` (`CustomAccountID` ASC):
Cannot resolve table name close to:
(`CustomAccountID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION
, ADD INDEX `FK_EBTx_CustomAccountID` (`CustomAccountID` ASC)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个 这里有很好的清单。
There's a nice checklist here.
根据我的经验,
errno: 150
通常表示密钥表和相关表中FOREIGN KEY
列的数据类型不相同。确保CustomAccounts.CustomAccountID
和ExpenseBackTransactions.CustomAccountID
的类型完全相同,包括UNSIGNED
(如果适用)。如果这没有帮助,请发布
SHOW CREATE TABLE ExpenseBackTransactions;
和SHOW CREATE TABLE CustomAccounts;
In my experience, the
errno: 150
usually indicates that the data types of theFOREIGN KEY
column in the key table and relating table are not identical. Make sure thatCustomAccounts.CustomAccountID
andExpenseBackTransactions.CustomAccountID
are the exact same type, includingUNSIGNED
if it applies.If that doesn't help, please post the
SHOW CREATE TABLE ExpenseBackTransactions;
andSHOW CREATE TABLE CustomAccounts;
陷阱 22. 外键需要索引。 MySQL 不会对该查询进行排序,因此在进行外键检查时索引就存在。因此,首先创建索引,然后在两个单独的查询中添加外键。
Catch 22. Foreign keys need indexes. MySQL doesn't order this query so that the index exists at the time it does it foreign key checks. Thus, first create the index, then add the foreign key in 2 separate queries.