简体   繁体   中英

How do you add a column in a rails migration in a specific location on the existing table?

I looked at these answers, and they don't seem to work:

I checked the API documentation , and I can't find a reference to first:, before: or after:.

Is it possible to add a column in a specific place on an existing table in Rails 5? I can't seem to get it to work.

Edit: here is the migration script

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

And the 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 

I know I could just move the user_id to the top of the list in the schema, or just redo the generator with it listed first, but I'm trying to learn, and I want to know if there is a way to do this with a migration.

您可以after: :a_field使用after: :a_field

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM