追溯性地将主键添加到表中

时间:2013-03-19 22:46:06

标签: ruby-on-rails activerecord

我为连接模型编写了一个迁移,它看起来像:

  create_table "project_memberships", :id => false, :force => true do |t|
    t.integer  "user_id"
    t.integer  "project_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
    t.integer  "id"
  end

我想现在强行创建ID。我必须删除表并重新创建它,还是可以编写一个删除此约束的迁移?

1 个答案:

答案 0 :(得分:2)

使用少量谷歌搜索... http://thinkwhere.wordpress.com/2009/05/09/adding-a-primary-key-id-to-table-in-rails/

生成空迁移:

rails generate migration AddIdToProjectMemberships 

并填写:

def change
    add_column :project_memberships, :id, :primary_key
end    

此前还有类似的问题.. how to add a primary key to a table in rails