Migrating from table without primary key to one with
I currently have a self-referencing join table without a primary key. If you're familiar with Rails, I have a has_many_and_belongs_to relationship defined.
The join table is called users_users. One FK is friend_id and the开发者_如何学运维 other is user_id.
I want to change the relationship of this table to has_many and belongs_to (again, if you're familiar with Rails). This means I need to a add a PK column.
What is the best way to do this?
Add a surrogate key
ALTER TABLE users_users ADD id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY
Should be all you have to do in MySQL, I don't know about anything different pertaining to your model setup in Rails.
精彩评论