Rails:“t.references"创建索引时不起作用 [英] Rails: "t.references" not working when creating index

查看:30
本文介绍了Rails:“t.references"创建索引时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

结果:

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'

我认为 t.references 应该为我处理这个问题?

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

推荐答案

您忘记添加_id",如下所示:

You forgot to add "_id" like this:

add_index :ballots, :user_id

或者,如果您希望它自动编入索引:

or, if you want it indexed automatically:

t.references :user, index: true

更多信息:add_index参考资料

HTH

这篇关于Rails:“t.references"创建索引时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆