【发布时间】:2018-03-24 17:24:35
【问题描述】:
我查看了这些答案,但它们似乎不起作用:
- In a Rails Migration (MySQL), can you specify what position a new column should be?
- Adding a column before another one in Rails
- ruby on rails add a column after a specific column name Ask
我检查了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