Rails: “t.references” not working when creating index

前端 未结 2 883
忘了有多久
忘了有多久 2020-12-18 23:15
class CreateBallots < ActiveRecord::Migration
  def change
    create_table :ballots do |t|
      t.references :user
      t.references :score
      t.references          


        
2条回答
  •  眼角桃花
    2020-12-18 23:40

    The answer above is correct, but be aware that:

    t.references :user, index: true is only available in Rails 4.0 & up.

    In earlier versions of Rails (3.x), index: true will fail silently, leaving you without an index on that table. For Rails 3.2.x & down, use the older syntax:

    add_index :ballots, :user_id

    Or in full using your example:

    class CreateBallots < ActiveRecord::Migration
      def change
        create_table :ballots do |t|
          t.references :user
          t.references :score
          t.references :election
          t.string :key
          t.timestamps
        end
        add_index :ballots, :user_id
        add_index :ballots, :score_id
        add_index :ballots, :election_id
      end
    end
    

提交回复
热议问题