how to change the position of a new column in rails 3.x migration

匆匆过客 提交于 2020-01-03 17:25:35

问题


I created a migration that simply adds a new column, but I want it to be added before the created_at and updated_at columns, is there a way to specify in which position the new column is created ?

I searched and only found someone saying to use the :after option in add_column, but it doesn't do anything. then I looked in the rails api docs and found no such option.


回答1:


After option works well for me

add_column :table_name, :column_name, :type, :after => :column_name

Also look at this question.




回答2:


From my research, the :after option does not work for SQLite or Postgres, as these DBMSs do not allow such operation. However, the option works for MySQL.

References:

  1. SQLite: https://www.sqlite.org/lang_altertable.html - this basically tells you to create a new table with correct column ordering, then copy data over.

  2. Postgres: https://wiki.postgresql.org/wiki/Alter_column_position



来源:https://stackoverflow.com/questions/9046413/how-to-change-the-position-of-a-new-column-in-rails-3-x-migration

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