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.
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.
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.
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.
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.
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;
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);
id_familjer is a primary key? set it.
The Referenced Column only display the familjer table primary key.
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.
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
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.
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.
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.
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
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)!
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!
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
© 2022 - 2024 — McMap. All rights reserved.