【问题标题】:How do you add a column in a rails migration in a specific location on the existing table?如何在现有表的特定位置在 Rails 迁移中添加列?
【发布时间】:2018-03-24 17:24:35
【问题描述】:

我查看了这些答案,但它们似乎不起作用:

我检查了API documentation,但找不到对 first:、before: 或 after: 的引用。

是否可以在 Rails 5 中现有表的特定位置添加列?我似乎无法让它工作。

编辑:这里是迁移脚本

class CreatePrivileges < ActiveRecord::Migration[5.1]
    def change
      create_table :privileges do |t|
        t.boolean :no_site_access
        t.boolean :edit_schedule

        t.timestamps
      end
    end
  end
class AddUserIdToPrivileges < ActiveRecord::Migration[5.1]
    def change
      add_column :privileges, :user_id, :integer, first: true
      add_index :privileges, :user_id
    end
end

还有 db/schema.rb

ActiveRecord::Schema.define(version: 20180324160753) do

      create_table "privileges", force: :cascade do |t| 
        t.boolean "no_site_access"
        t.boolean "edit_schedule"
        t.datetime "created_at", null: false
        t.datetime "updated_at", null: false
        t.integer "user_id"
        t.index ["user_id"], name: "index_privileges_on_user_id"
      end 
    end 

我知道我可以将 user_id 移动到架构中列表的顶部,或者只是重做生成器并首先列出它,但我正在努力学习,我想知道是否有办法通过迁移来做到这一点。

【问题讨论】:

  • 能否将您的迁移脚本添加到您的帖子中?

标签: ruby-on-rails ruby-on-rails-5


【解决方案1】:

您可以使用after: :a_field

  add_column :privileges, :user_id, :integer, after: :no_site_access

【讨论】:

    猜你喜欢
    • 2011-06-17
    • 2016-05-24
    • 1970-01-01
    • 2021-10-22
    • 2015-05-06
    • 2011-02-25
    • 2013-05-23
    • 2018-05-29
    • 1970-01-01
    相关资源
    最近更新 更多