ruby on rails add a column after a specific column name

房东的猫 提交于 2019-11-30 01:15:07

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/15481733/ruby-on-rails-add-a-column-after-a-specific-column-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!