Eric Baldwin
Eric Baldwin

Reputation: 3491

Retroactively adding the primary key to a table

I wrote a migration for a join model which looks like:

  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

I want to forcibly create ids now. Must I drop the table and recreate it or can I write a migration removing this constraint?

Upvotes: 1

Views: 507

Answers (1)

Zippie
Zippie

Reputation: 6088

With a small amount of googling... http://thinkwhere.wordpress.com/2009/05/09/adding-a-primary-key-id-to-table-in-rails/

Generate a empty migration:

rails generate migration AddIdToProjectMemberships 

and fill it in with:

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

Also there was a question like this before.. how to add a primary key to a table in rails

Upvotes: 2

Related Questions