Reputation: 18540
I have a Rails app, where one of the models does not have the id
column. Doing some research I found the migration that created it:
create_table(:the_model, :id => false) do |t|
# columns
end
Now, on a new migration, I want to add the id
column in a Rails standard way (not using database specific sql). How can I do that?
I already tried this without success:
change_table(:the_model, :id => true) do |t|
end
Upvotes: 23
Views: 25330
Reputation: 8892
You can either manually add the id column:
add_column :table_name, :id, :primary_key
or clear (or backup) your data, rollback to this migration, get rid of the option :id => false
, and re-migrate.
Upvotes: 49
Reputation: 81
You don't need to mention :integer
rails g migration add_id_to_model id:primary_key
worked for me.
Upvotes: 8
Reputation: 5317
You already got your answer, but here is a one liner that does all in this case
rails generate migration AddIdToModel id:integer
Look at the syntax of migration file name AddColumnNameToTableName followed the column description. It will generate something like below
class AddIdToModel < ActiveRecord::Migration
def change
add_column :models, :id, :integer
end
end
Now you can change this line if you feel for anything else. and just run rake db:migrate
.
Upvotes: 4