Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL foreign key cannot be created (index problems?)

Tags:

mysql

I have a little problem making some foreign keys...
When I try to define the foreign key in MySQL Workbench, I get the following message:

Selected column 'playerName' must be indexed and be of a compatible type for a Foreign Key to be created.

There my problem starts: I'm pretty sure, that the column (towns.playerName) is indexed and it's definitively of the same type ( VARCHAR(255) )...
Indexes of 'towns'
I want to add a Foreign Key from players.name (primary key, not null, unique) to towns.playersName(not null).
So what can I do to get the foreign key created?
It seems i am doing something wrong...

PS: I'm sorry, if there is already a question for this...

EDIT: I just tried again (exactly as I did before several times) and now it works... really strange
Perhaps a bug in MySQL Workbench??

like image 801
Sa'Kagé Avatar asked Sep 29 '22 16:09

Sa'Kagé


1 Answers

I got the same problem a number of times, but finally found a interesting and useful concept which I missed while learning mysql. If a column is not a 'key' in the table 1, you cannot add it as foreign key unless it should have an index. So I make the column a indexed column.

e.g.

CREATE INDEX any_name ON table1 (column1);

finally, I was able to solve my problem.

like image 85
ASK Avatar answered Oct 02 '22 13:10

ASK