1

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?

Zippie
  • 6,018
  • 6
  • 31
  • 46
Eric Baldwin
  • 3,341
  • 10
  • 31
  • 71
  • possible duplicate of [how to add a primary key to a table in rails](http://stackoverflow.com/questions/9644509/how-to-add-a-primary-key-to-a-table-in-rails) – ABMagil Nov 09 '14 at 15:52
  • Thanks for the note, @ABMagil. I want to leave this question here because in my case there was already an `id` column (which is not true in your link). However, I am open to closing it if this isn't a significant difference to the StackOverflow community. – Eric Baldwin Nov 09 '14 at 17:53

1 Answers1

2

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

Community
  • 1
  • 1
Zippie
  • 6,018
  • 6
  • 31
  • 46