Rails 4迁移:如何对列进行重新排序 [英] Rails 4 migration: how to reorder columns
问题描述
我了解到add_column
有一个:after
选项可以设置插入列的位置.太糟糕了,我是在添加一堆之后才知道的.
I learned that add_column
has an :after
option to set where the column gets inserted. Too bad I learned about it :after adding a bunch.
如何编写迁移以仅对列进行重新排序?
How can I write a migration to simply reorder columns?
推荐答案
您可以调用change_column
,但是您必须重复列类型(只需从其他迁移中复制并粘贴):
You can call change_column
, but you have to repeat the column type (just copy and paste it from your other migration):
def up
change_column :your_table, :some_column, :integer, after: :other_column
end
或者如果您必须对一个表中的多列进行重新排序:
Or if you have to reorder multiple columns in one table:
def up
change_table :your_table do |t|
t.change :some_column, :integer, after: :other_column
# ...
end
end
change_column
在后台调用ALTER TABLE
.从MySQL 文档:
change_column
calls ALTER TABLE
under the hood. From the MySQL documentation:
您还可以在
CHANGE
或MODIFY
操作中使用FIRST
和AFTER
重新排列表格中的列.
You can also use
FIRST
andAFTER
inCHANGE
orMODIFY
operations to reorder columns within a table.
这篇关于Rails 4迁移:如何对列进行重新排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!