40

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

1
  • 2
    it has still the same problem after 7 years
    – Asqan
    Dec 23, 2019 at 14:09

13 Answers 13

60

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.

8
  • This was my problem. Thanks! Had set the NN and Unsigned on primary key but forgot on the foreign key before setting the constraint
    – nils
    Feb 5, 2016 at 0:55
  • 4
    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! TY
    – hammus
    Jun 8, 2016 at 3:36
  • 1
    Another reason could be different table collations.
    – shaahiin
    Jul 10, 2019 at 4:29
  • 2
    Didn't work for me.
    – Eagle_
    Jul 26, 2022 at 11:42
  • 1
    @Eagle_ maybe shahiin's comment above helps?
    – dwkd
    Aug 5, 2022 at 19:57
11

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.

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.

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;
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).
    – JoeRaid
    Oct 29, 2019 at 15:43
  • This solution worked for me. Looks like DB Model is broken/outdated then? Any tips on how to regenerate this?
    – Eagle_
    Jul 26, 2022 at 11:43
2

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

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!
    – Shrout1
    May 24, 2022 at 18:53
2

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

2
  • 10
    Could you please add some information on yhow to manually correct this?
    – s.Daniel
    Dec 4, 2012 at 10:51
  • @s.Daniel Generate SQL script and then manually update too long index names in your favorite editor. May 1, 2019 at 15:24
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

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.

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.

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.

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

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)!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.