I have many-to-many linking table between two entities, datacenters and projects,
in legacy code. I discovered, that it’s really a one to many relationship.
As a first step to cleaning up the relationship, I wanted to put a unique index
on one of the fields.
Now I’m getting the following error:
Primary key is not allowed in a has_and_belongs_to_many join table (datacenters_projects).
I’m using Rails 2.3.8 running on ruby 1.8.7p174 and mysql Ver 14.12 Distrib 5.0.77.
This is not a primary key, it’s just a unique index.
I suppose it’s a candidate key, but I haven’t decided it is a primary key.
I don’t know why mysql considers this to be a primary key.
I don’t know why ActiveRecord would object to the linking table in a
has_and_belongs_to_many having a primary key.
But my real question is, “Is there any way I can put a unique index on
one of the fields, without ActiveRecord giving me an error?”
Is there any way I can tell add_index that this is not a primary key?
I’m not sure why that would fail. You should be able to get around it using SQL in your migration though, e.g: