Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: "t.references" not working when creating index

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
    add_index :ballots, :score
    add_index :ballots, :election
  end
end

results in:

SQLite3::SQLException: table ballots has no column named user: CREATE  INDEX "index_ballots_on_user" ON "ballots" ("user")/home/muhd/awesomevote/db/migrate/20130624024349_create_ballots.rb:10:in `change'

I thought t.references was supposed to handle that for me?

like image 905
Muhd Avatar asked Jul 04 '13 22:07

Muhd


1 Answers

You forgot to add "_id" like this:

add_index :ballots, :user_id

or, if you want it indexed automatically:

t.references :user, index: true

More info: add_index , references

HTH

like image 90
a.s.t.r.o Avatar answered Oct 15 '22 02:10

a.s.t.r.o