向现有表添加外键会出现错误 1050 表已存在

发布于 2024-11-28 17:52:51 字数 849 浏览 1 评论 0原文

我有一个带有列的表 CustomizationSet:

customization_set_guid (which is a non-nullable guid and also the primary key)
creator_account_guid
and a few others

以及一个带有现有数据的表 带有列的注册:

registration_id (an int and the primary key)
customization_set_guid (also a guid (so a char(36)) which is nullable, and all entries are currently null)
and a few other columns

当我尝试

ALTER TABLE Registration ADD FOREIGN KEY 
    (
        customization_set_guid
    ) REFERENCES CustomizationSet (
        customization_set_guid
    );

在 MySQL Workbench 中运行时,它给出错误 1050Table '.\dbname\registration' 已存在。

如果我尝试使用 UI 通过“更改表”对话框的“外键”选项卡添加外键,并选择 CustomizationSet 作为引用表,它不会让我在列列表中选择customization_set_guid。

我真的不确定为什么它不允许我添加这个外键。我刚刚在刚刚添加的表之间成功创建了外键。注册表已经存在一段时间了......

I've a table CustomizationSet with the columns:

customization_set_guid (which is a non-nullable guid and also the primary key)
creator_account_guid
and a few others

And a table with existing data Registration with the columns:

registration_id (an int and the primary key)
customization_set_guid (also a guid (so a char(36)) which is nullable, and all entries are currently null)
and a few other columns

When I try and run

ALTER TABLE Registration ADD FOREIGN KEY 
    (
        customization_set_guid
    ) REFERENCES CustomizationSet (
        customization_set_guid
    );

in MySQL Workbench, it gives the error 1050Table '.\dbname\registration' already exists.

If I try to use the UI to add the foreign keys with the Foreign Keys tab of the Alter Table Dialog, and choose CustomizationSet as the referenced table, it doesn't let me choose customization_set_guid in the list of columns.

I'm really not sure why it won't let me add this foreign key. I've just successfully created foreign keys between tables I just added. The Registration table has existed for awhile...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

各空 2024-12-05 17:52:51

我遇到了同样的错误,这是由于外键已经存在。你想要的只是添加约束:

ALTER TABLE Registration 
  ADD CONSTRAINT idx_Registration_CustomizationSet 
  FOREIGN KEY (customization_set_guid) 
  REFERENCES CustomizationSet(customization_set_guid);

I got the same error, and it was due to the fact that the foreign key already existed. What you want is just to add the constraint:

ALTER TABLE Registration 
  ADD CONSTRAINT idx_Registration_CustomizationSet 
  FOREIGN KEY (customization_set_guid) 
  REFERENCES CustomizationSet(customization_set_guid);
网白 2024-12-05 17:52:51

看起来 MySQL 上有一个错误报告,位于此处:

MySQL Bug 55296

最后,我猜他们升级了服务器并解决了问题。从阅读来看,我不确定。他们确实有一些解决方法,例如输入约束名称/更改它们。如果您认为这是相同的,我会要求重新打开该错误。

有一次,他们提到类型不匹配,工作台响应错误错误(应该是 errno 150 或 errno 121)。您可以在此处查看这些错误的原因:
MySQL 外键错误和 Errno 150

It looks like there is a bug report for this at MySQL located here:

MySQL Bug 55296

In the end, I guess they upgraded their server and it fixed the issue. From reading it, I'm not sure though. They did have some workarounds like putting in constraint names/changing them. If you think this is the same, I would request that the bug is reopened.

At one point, they mention the types didn't match and workbench was responding with the wrong error (it should have been an errno 150, or errno 121). You can see the causes for those errors here:
MySQL Foreign Key Errors and Errno 150

写给空气的情书 2024-12-05 17:52:51

所以一个团队成员想出了这个办法。其中一个表设置为 utf8_general 类型,另一个表设置为默认类型。我不认为这是一个问题,因为默认值是 utf8_general,但显然 mysql 只查看类型名称而不是底层类型。

So a team member figured this out. The one table was set with the type utf8_general, and another was set to the type default. I didn't think this was an issue, since the default is utf8_general, but apparently mysql just looks at the type names and not the underlying type.

紫轩蝶泪 2024-12-05 17:52:51

我遇到了同样的错误,由于尚未提及我的情况,我将发布此答案,希望它可以节省某人的时间!

我的两个表引擎不同。
一个是 InnoDB,另一个是 MyIsam。

要更改表的引擎:

选择表,点击更改表,然后点击双箭头
工作台的最右侧(因此它将指向上方)。

现在更换引擎!

I got the same error, and since my case wasnt mentioned yet, i ll post this answer and hopefully it may save somebody's time!

My two tables engines, where different.
The one was InnoDB, and the other MyIsam.

To change the engine of a table:

choose table, hit alter table, and then to hit that double arrow at
the right-most of the Workbench(so it will point upwards).

Now change the engine!

南薇 2024-12-05 17:52:51
  • 检查 CustomizationSet 表的存储引擎类型。

我遇到了同样的问题,但我可以通过将引擎类型更改为来解决它
InnoDB ,因为很少有类型不支持外键约束。

  • Check the Storage Engine type for CustomizationSet table.

I had a same issue but i could solve it by changing engine type to
InnoDB , because few types don't support foreign key constraints.

檐上三寸雪 2024-12-05 17:52:51

不确定表是否已经存在,但它不允许您选择所需列的原因很可能是由于列不是同一类型。检查以确保它们具有相同的类型、相同的长度并且具有相同的选项。

Not sure about the table already existing, but the reason it's not letting you choose the column you want is most likely due to the columns not being the same type. Check to ensure they are both the same type, same length, and have all the same options.

标点 2024-12-05 17:52:51

我不确定这是否是一个错字,但不应该

ALTER TABLE Registration ADD FOREIGN KEY 
(
    customization_set_guid
) REFERENCES CustomizationSet (
    customization_set_guid
);

是这样的

ALTER TABLE Registration ADD FOREIGN KEY 
customization_set_guid_fk (customization_set_guid) 
REFERENCES CustomizationSet (customization_set_guid);

I'm not sure if it's a typo but shouldn't be

ALTER TABLE Registration ADD FOREIGN KEY 
(
    customization_set_guid
) REFERENCES CustomizationSet (
    customization_set_guid
);

be something like

ALTER TABLE Registration ADD FOREIGN KEY 
customization_set_guid_fk (customization_set_guid) 
REFERENCES CustomizationSet (customization_set_guid);
谈情不如逗狗 2024-12-05 17:52:51

我也遇到了类似的问题,最终是完整性约束的问题。
外键列引用了一个没有引用的外键列
存在。

尝试运行以下命令来测试是否是这种情况:

select r.customization_set_guid, c.customization_set_guid
from Registration r
right join CustomizationSet c
on 
r.customization_set_guid = c.customization_set_guid
where isnull(c.customization_set_guid);

I had a similar problem and in the end it was a problem of Integrity Constraint.
The Foreign Key column was referencing a foreign column that didnt
exist.

Try run the following to test whether this is the case:

select r.customization_set_guid, c.customization_set_guid
from Registration r
right join CustomizationSet c
on 
r.customization_set_guid = c.customization_set_guid
where isnull(c.customization_set_guid);
困倦 2024-12-05 17:52:51

使用 MysqlWorkbench 时,该错误具有误导性。我的问题是,我试图在已经有行且其中一行为空的表上添加外键约束(不满足 FK 约束)。MysqlWorkbench 没有抱怨如果应用该约束就会失败,而是报告该表存在。

删除固定的有问题的行(或向字段添加和约束可接受的值)解决了问题。

When using MysqlWorkbench the error is misleading. My issue was that I was trying to add a foreign key constraint on table that already had rows and one of the rows was empty (did not meet the FK constraint. Instead of complaining that constraint will fail if applied, MysqlWorkbench reported that table exists.

Removing the offending row fixed (or adding and constraint acceptable value to the field) solved the problem.

衣神在巴黎 2024-12-05 17:52:51

简短:
由于以下原因,我遇到了此错误:“外键名称在整个数据库中必须是唯一的”

如果您没有为外键指定名称,则自动生成的名称会遇到相同的问题。

背景:
在我的修复活动中,我发现了这一点,这给了我修复提示:
写入或更新时出现重复密钥?

在过去,我确实与那个外国人作过斗争键约束,因此在同一数据库中有不同的“测试表”。其中一个外键的名称完全相同,因此该名称被阻止。
删除我的测试表解决了这个问题。重命名密钥也可能做到这一点。

Short:
I ran into this error due to the following reason: "Foreign key names must be unique within the whole database"

If you do not give a name for the foreign key, the auto-generated name runs into the same issue.

Background:
During my fixing activities I found this, which gave me the fixing hint:
Duplicate key on write or update?

In that past I did fight against that foreign key constraint and thus had different "test tables" within the same database. One had the foreign key with exaclty the same name and thus that name was blocked.
Dropping my test tables solved it. Renaming the key might have done it too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文