Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Id column in a migration

Tags:

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 
like image 872
alf Avatar asked Sep 23 '12 00:09

alf


People also ask

What is ActiveRecord migration?

Migrations are a feature of Active Record that allows you to evolve your database schema over time. Rather than write schema modifications in pure SQL, migrations allow you to use a Ruby DSL to describe changes to your tables.

How to run migration in rails?

Active Record tracks which migrations have already been run so all you have to do is update your source and run rake db:migrate. Active Record will work out which migrations should be run. It will also update your db/schema. rb file to match the structure of your database.

What is db Migrate?

A migration means that you move from the current version to a newer version (as is said in the first answer). Using rake db:migrate you can apply any new changes to your schema. But if you want to rollback to a previous migration you can use rake db:rollback to nullify your new changes if they are incorrectly defined.


1 Answers

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.

like image 127
cdesrosiers Avatar answered Nov 02 '22 21:11

cdesrosiers