Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby on rails add a column after a specific column name

Tags:

I tried to add a column to a table after a specific column in the table. Here is what I did:

rails generate migration add_reaction_id_to_patient_allergies reaction_id: integer :after => 'patient_id' 

Here is what my migration file looks like:

class AddReactionIdToPatientAllergies < ActiveRecord::Migration   def change     add_column :patient_allergies, :reaction_id, :string     add_column :patient_allergies, :integer, :string     add_column :patient_allergies, :, :after     add_column :patient_allergies, :=, :string   end end 

I dont think the command went well. I see an '=' in the above file. I do not think it should be there. Can someone tell me if I missed anything?

If so , how do I undo the above?

like image 229
Micheal Avatar asked Mar 18 '13 16:03

Micheal


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 ...

How do I add a reference column in rails?

When you already have users and uploads tables and wish to add a new relationship between them. Then, run the migration using rake db:migrate . This migration will take care of adding a new column named user_id to uploads table (referencing id column in users table), PLUS it will also add an index on the new column.

How do you rename a column in Ruby on Rails?

If you happen to have a whole bunch of columns to rename, or something that would have required repeating the table name over and over again: rename_column :table_name, :old_column1, :new_column1 rename_column :table_name, :old_column2, :new_column2 ...


1 Answers

I doubt it allowed you to actually rake db:migrate this migration, so you shouldn't have to roll back. Just remove the bottom three add_columns and replace the top one with

add_column :patient_allergies, :reaction_id, :integer, after: :patient_id 

and it should be fine to migrate. For future reference, here's what that command you entered should look like:

rails generate migration add_reaction_id_to_patient_allergies reaction_id:integer 

The space before integer made the generator think it was a new column. Sadly you can't use Ruby syntax (a => b) on the command line either.

like image 105
piersadrian Avatar answered Nov 03 '22 00:11

piersadrian