Adding foreign key to existing table gives error 1

2019-02-16 05:35发布

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...

9条回答
你好瞎i
2楼-- · 2019-02-16 06:01

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.

查看更多
We Are One
3楼-- · 2019-02-16 06:01

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);
查看更多
不美不萌又怎样
4楼-- · 2019-02-16 06:03

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);
查看更多
Explosion°爆炸
5楼-- · 2019-02-16 06:04

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

查看更多
够拽才男人
6楼-- · 2019-02-16 06:07

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!

查看更多
SAY GOODBYE
7楼-- · 2019-02-16 06:12

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);
查看更多
登录 后发表回答