Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding multiple columns in one migration

Scenario: we already have a Document model, and we want to add

  • public
  • private

or more columns using a single migration.

From what I've searched and read so far, you just have to edit the migration file. If the migration was already applied, roll back and migrate again.

  1. Is this the common best practice, or is there a better way?
  2. If this is the way to do it, what would a proper naming for that migration be?
like image 248
Marius Butuc Avatar asked Feb 14 '11 21:02

Marius Butuc


People also ask

How do you add multiple columns in rails?

command to create new model and table with columns : rails g model ModelName col_name1:string col_name2:integer col_name3:text ... command to add more columns under existing table: rails g migration AddColumnToModelName col_name4:string col_name5:integer ...


1 Answers

Rolling back a migration and re-editing it is only safe if that migration is local and has not been pushed out to any repository. Editing a migration that others have applied may cause issues.

The safest way to add columns to an existing table is to just create a new migration:

rails g migration add_public_and_private_to_document public:string private:string

If you use the add_[column_names]_to_[model] naming convention, rails will work out the appropriate table and create the migration you want.

Have a read here for more info: http://guides.rubyonrails.org/migrations.html

like image 106
Cam Avatar answered Oct 04 '22 04:10

Cam