开发者

Migrating from table without primary key to one with

开发者 https://www.devze.com 2023-03-03 11:24 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号