gpt4 book ai didi

ruby-on-rails - rails : "t.references" not working when creating index

转载 作者:行者123 更新时间:2023-12-04 00:01:08 25 4
gpt4 key购买 nike

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应该为我处理那个?

最佳答案

你忘了像这样添加“_id”:

add_index :ballots, :user_id

或者,如果您希望它自动索引:
t.references :user, index: true

更多信息: add_index , references

HTH

关于ruby-on-rails - rails : "t.references" not working when creating index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17478669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com