Rails 4迁移:如何对列进行重新排序 [英] Rails 4 migration: how to reorder columns

查看:63
本文介绍了Rails 4迁移:如何对列进行重新排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到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:

您还可以在CHANGEMODIFY操作中使用FIRSTAFTER 重新排列表格中的列.

You can also use FIRST and AFTER in CHANGE or MODIFY operations to reorder columns within a table.

这篇关于Rails 4迁移:如何对列进行重新排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆