简体   繁体   English

两个模型之间有多个belongs_to has_many:SQLException:没有这样的表

[英]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.

相关问题 查询两个表之间的belongs_to / has_many关系 - Query on two tables with belongs_to/has_many relation Ruby on rails-使用JUST'belongs_to'与使用BOTH'has_many'和'belongs_to'之间的区别? - ruby on rails - difference between using JUST 'belongs_to' versus using BOTH 'has_many' and 'belongs_to'? Rails:从2个模型中选择具有Unique_to和has_many关系的唯一值 - Rails: select unique values from 2 models with belongs_to and has_many relation 在Rails中以belongs_to和has_many关系从多个表中获取数据 - Getting data from multiple tables in a belongs_to and has_many relationship in Rails 在同一模型之间使用多个has_many关系 - rails multiple has_many relationships between the same models Rails 4“where” - 查询“has_many belongs_to” - 关系,搜索特定计数 - Rails 4 “where”-query with “has_many belongs_to”-relationship, searching with specific count 如何使用DBIx :: Class创建嵌套的has_many或belongs_to关系? - How do I create a nested has_many or belongs_to relationship with DBIx::Class? 如何为此逻辑编写一个SQL查询(has_many,belongs_to关联)? - How to write a SQL query for this logic (has_many, belongs_to association)? 使用has_many和belongs_to关系,我需要在一个索引视图中显示每个资源的参数 - Using a has_many and belongs_to relationship, I need to display the parameters of each resource in one index view 通过named_scope返回对象数组 - has_many ... belongs_to association; UNION ALL查询 - Returning array of objects via named_scope — has_many…belongs_to association; UNION ALL query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM