[英]Multiple belongs_to has_many between two models : SQLException: no such table
I have two models linked with each other by two has_many
/ belongs_to
association with a foreign key, but I have some trouble to save any fight object. 我有两个通过两个具有外键的
has_many
/ belongs_to
关联彼此链接的模型,但是在保存任何战斗对象时遇到了一些麻烦。
Here my models: 这是我的模型:
Fight.rb Fight.rb
belongs_to :winner, class_name: 'Fighter', foreign_key: :winner_id, inverse_of: 'winned_fights'
belongs_to :looser, class_name: 'Fighter', foreign_key: :looser_id, inverse_of: 'loosed_fights'
Fighter.rb Fighter.rb
has_many :winned_fights, class_name: 'Fight', foreign_key: :winner_id, inverse_of: 'winner'
has_many :loosed_fights, class_name: 'Fight', foreign_key: :looser_id, inverse_of: 'looser'
db/schema.rb DB / schema.rb
create_table "fights", force: :cascade do |t|
...
t.integer "winner_id"
t.integer "looser_id"
t.index ["looser_id"], name: "index_fights_on_looser_id"
t.index ["winner_id"], name: "index_fights_on_winner_id"
end
create_table "fighters", force: :cascade do |t|
t.string "name"
...
end
This is what happens when I try to save a Fight object: 当我尝试保存Fight对象时会发生以下情况:
ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: main.loosers: INSERT INTO "fights" ("winner_punches", "looser_punches", "victory_type", "rounds", "winner_id", "looser_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?)):
Here is the SQL request that fails: 这是失败的SQL请求:
Fight Create (1.5ms) INSERT INTO "fights" ("winner_punches", "looser_punches", "victory_type", "rounds", "winner_id", "looser_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["winner_punches", 1], ["looser_punches", 1], ["victory_type", 0], ["rounds", 1], ["winner_id", 18], ["looser_id", 2], ["created_at", "2018-10-15 15:46:13.479488"], ["updated_at", "2018-10-15 15:46:13.479488"]]
I can't figure out if the problem come from my has_many
/ belongs_to
association declaration or if it is a DB problem. 我不知道问题是否出在我的
has_many
/ belongs_to
关联声明中,或者是数据库问题。
What's currently working: 目前正在做什么:
figther.winned_fights
figther.loosed_fights
Does someone would guide me to the right path ? 有人会引导我走正确的道路吗?
First thing, I don't think you need to put the foreign_key option in the fight model, as it's usually taken from the association name. 第一件事,我认为您不需要在战斗模型中放置Foreign_key选项,因为它通常是从关联名称中获取的。
Now, the problem is actually in the database, when you wrote or generated the migration to build the model, you probably used a belongs_to field (or references field), which automatically creates a foreign key constraint on the database. 现在,问题出在数据库中,当您编写或生成迁移以构建模型时,您可能使用了一个belongs_to字段(或引用字段),该字段会自动在数据库上创建外键约束。
So you have a foreign key constraint from the column looser_id to a table (automatically guessed) loosers. 因此,您从列lacker_id到表(自动猜测)的松散器都具有外键约束。 You should check your schema.rb to see if this is correct, in which case, just remove that constraint or fix it using another migration : foreigner - remove foreign key
您应该检查schema.rb以查看是否正确,在这种情况下,只需删除该约束或使用另一个迁移即可解决此问题: Foreigner-删除外键
You should always try to stick to migrations to work with db modifications. 您应该始终尝试使用迁移来进行数据库修改。
you can add the foreign keys to the respective tables after they have been created and it will properly index it in Rails 4+ 您可以在创建外键后将其添加到各个表中,并将在Rails 4+中正确对其进行索引
rails g migration addWinnerReferencesToFights winner_id:references
rails g migration addLooserReferencesToFights looser_id:references
PS - it also helps a lot if you spell the models correctly ie: there is no english word called winned nor loosed >>> correct plurals are wins and losses (loose means something not tight or affixed) PS-如果您正确拼写模型,这也会有很多帮助,例如:没有英文单词winned或松散>>>正确的复数形式是赢与输(松散是指没有紧紧或粘贴的东西)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.