0

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.

smilingfrog
  • 376
  • 3
  • 14

1 Answers1

0

You can use after: :a_field

  add_column :privileges, :user_id, :integer, after: :no_site_access
Dapeng114
  • 207
  • 2
  • 10