Can't set foreign key relationship
Asked Answered
M

15

42

I have some problem with MySQL Workbench in that I sometimes can't set foreign keys when creating tables. I say sometimes cause it's not always like this. The thing is when I enter a FK and choose a reference table I can't pick a referenced column. I can't click the check box and the drop down list is empty. I can't really figure out what the problem is cause I see no real difference from the FK's that are working. I have checked data type, name etc and they are correct. I'll provide a SS to elaborate. The green marked key (id_hem) is working ok and the red marks are those that don't.

Screenshot of WB

Miscarriage answered 23/6, 2012 at 20:30 Comment(2)
it has still the same problem after 7 yearsLurid
it has still the same problem after 11 yearsSpier
B
1

I had the same problem and the issue was in foreign key indexes. MySQL workbench generates too long names for fk indexes sometimes. Manual correction helps.

Bijection answered 23/6, 2012 at 20:38 Comment(2)
Could you please add some information on yhow to manually correct this?Achernar
@Achernar Generate SQL script and then manually update too long index names in your favorite editor.Bijection
A
63

I know this is old but the usual culprits are Non Null flag and Unsigned. Make sure you do match these on the foreign key column if the referenced column does have them enabled.

Altruistic answered 3/3, 2015 at 12:28 Comment(9)
This was my problem. Thanks! Had set the NN and Unsigned on primary key but forgot on the foreign key before setting the constraintPledget
This is why I love SO. 3 years on and someone figures out what causes a bug that still hasnt been fixed and solves it! TYRestore
Another reason could be different table collations.Binghi
what a time saver!Cazzie
Why do we need to match the Non Null flag? A foreign key column can be null right? This problem is occurring for me, but I don't want to make my foreign key column Non NullRosenkrantz
Didn't work for me.Arduous
@R.S.K a foreign key guaranties data integrity across multiple tables so it has to have the exact constraints as the primary key it referencesAltruistic
@Arduous maybe shahiin's comment above helps?Altruistic
I don't understand why the Workbench doesn't provide a warning message for this mismatch and then load a prompt to align FK's flags to the PK it is pointing to (or vice-versa)?Constantin
G
12

It can be also because of different collations. Just check if those 2 columns have the same collation.

In my case one was with

Table default (using utf8 general)

the other one with

utf8_unicode_ci

I have set those 2 columns with utf8_unicode_ci and it works.

George answered 22/9, 2016 at 11:30 Comment(0)
F
9

I had the same problem.. then I have checked the two tables.. The issue was the type, it should be matched in both two tables.

if in foralda table column id_familjer is (INT)
in fkblixten table column id_familjer also should be be (INT). if the two data types are not matching MySQL Workbench won't allow to select the column as foreign key.

Furcula answered 17/8, 2013 at 5:22 Comment(0)
E
4

In accepted answer Manual correction in the sense: First create a index for the referencing key in the referenced table. (you can do that in the indices tab available at each table). Then, after clicking the apply ( even without selecting referenced column) change the code in the apply accordingly.

example:

ALTER TABLE `SCHEMA_NAME`.`SECOND_TABLE_NAME` (we are creating foreign key in this second table)
ADD CONSTRAINT `FOREIGN_KEY_NAME`
  FOREIGN KEY (`COLUMN_IN_SECOND_TABLE`)
  REFERENCES `SCHEMA_NAME`.`FIRST_TABLE_NAME` (`COLUMN_IN_FIRST_TABLE`)
  ON DELETE NO ACTION
  ON UPDATE NO ACTION;
Epithelioma answered 22/1, 2019 at 14:55 Comment(2)
Yes, a script is what eventually worked for me and the easiest way to create one is to create a new model, recreate all tables in question and their relationships in it, then finally have workbench generate the script for you using forward engineering (just stop at the review script phase and pick up the SQL and clean it from the unnecessary stuff because it will also try to create a schema and all the mentioned tables).Hellkite
This solution worked for me. Looks like DB Model is broken/outdated then? Any tips on how to regenerate this?Arduous
A
4

Even though from table properties I haven't managed to change the foreign key value, I tried using SQL code and I did it.

Code:

ALTER TABLE Table2
ADD FOREIGN KEY (Table2_fk_field) REFERENCES Table1(Table1_pk_field);
Ardelia answered 5/2, 2020 at 15:4 Comment(0)
A
2

id_familjer is a primary key? set it.
The Referenced Column only display the familjer table primary key.

Athanor answered 24/6, 2012 at 1:19 Comment(1)
I just had this problem and it took far longer to solve than it should have... Even more than 10 years after you answered I'm still bumping my head into this - go figure! Thanks so much!Underbelly
B
1

I had the same problem and the issue was in foreign key indexes. MySQL workbench generates too long names for fk indexes sometimes. Manual correction helps.

Bijection answered 23/6, 2012 at 20:38 Comment(2)
Could you please add some information on yhow to manually correct this?Achernar
@Achernar Generate SQL script and then manually update too long index names in your favorite editor.Bijection
T
1

Foreign key should have same constants similar to Primary key. You can ignore AI ,Primary Key. If the Primary key has UI , foreign should also have UI

UI-> Unsigned Interger AI-> Auto Increment

Threaten answered 18/12, 2016 at 21:5 Comment(0)
E
0

In my case, I change the 'collation' in every columns that I want to set the foreign keys.

For example, My FK column collation is 'utf8', I also change my reference table into 'utf8' too. And it's solved my problem.

I hope it's helpful.

Ejecta answered 22/12, 2016 at 4:18 Comment(0)
C
0

Also to add my two cents, this also happens if you are making EER diagrams in the same database namespace and two or more diagrams contain a table with the same name.

So if you attempted to make a relation to a table (that also exists in another diagram with the same name) it will show up as twice in the reference table, both refer to your current diagram schema and it won't allow you to set a relation.

The solution is to rename the table in the other diagram.

Cagliostro answered 26/1, 2017 at 6:53 Comment(0)
D
0

Make sure the data types are the same in both tables as if one says int and the other is int(6) it wont work as one has a limit. I had this problem and that is how I fixed it.

Damal answered 16/8, 2018 at 14:20 Comment(0)
L
0

None of them worked... So I wrote the sql query myself in to set foreign key. It worked without any problem. After that, I could select the property (which I used in my query) in "Referenced Column" in right hand side.

Weird

Lurid answered 23/12, 2019 at 15:2 Comment(0)
T
0

In my case, setting the constraint with existing data, I forgot to first check for and delete the rows in the child table that would violate the constraint (the foreign key was not found in the parent table)!

Tipstaff answered 9/2, 2021 at 19:17 Comment(0)
H
0

I FOUND! If your refered table contains JSON field it's blocking ability to select columns from that table as foreign keys!!! workaround I switched those fields to TEXT and suddenly it worked!

Heterolecithal answered 31/1 at 23:48 Comment(0)
P
0

I figured out a solution, i had my db set up like (table1 reference -> table2 reference -> table3). so there are multiple foregin keys. its strange but i got it working only when adding the relations from (table3 <- table2 <- table1), so just delete the foreign keys and add them from the ta

Piceous answered 2/6 at 17:50 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Makalu

© 2022 - 2024 — McMap. All rights reserved.