多个属于两个模型之间有多个:SQLException:没有这样的表

多个属于两个模型之间有多个:SQLException:没有这样的表,sql,ruby-on-rails,ruby,sqlite,Sql,Ruby On Rails,Ruby,Sqlite,我有两个模型通过两个相互链接,有许多/属于与外键的关联,但我在保存任何战斗对象时遇到了一些麻烦 以下是我的模型: 战斗。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_fig

我有两个模型通过两个
相互链接,有许多
/
属于与外键的关联,但我在保存任何战斗对象时遇到了一些麻烦

以下是我的模型:

战斗。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'
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'
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
战斗机.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'
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'
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
db/schema.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'
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'
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
这是我试图保存战斗对象时发生的情况:

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 (?, ?, ?, ?, ?, ?, ?, ?)):
以下是失败的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"]]
我无法确定问题是否来自我的
有许多
/
属于
关联声明,或者它是否是DB问题

目前正在运行的功能:

  • figther.赢得的比赛
  • figther.loosed\u打架

有人会引导我走上正确的道路吗?

首先,我认为你不需要在战斗模型中加入外键选项,因为它通常取自协会名称

现在,问题实际上在数据库中,当您编写或生成迁移以构建模型时,您可能使用了一个“属于”字段(或“引用”字段),该字段会自动在数据库上创建一个外键约束

因此有一个外键约束,从列looser_id到表(自动猜测)looser。您应该检查schema.rb,看看这是否正确,在这种情况下,只需删除该约束或使用另一个迁移来修复它:

您应该始终坚持迁移以使用db修改

您可以在创建外键后将它们添加到相应的表中,它将在Rails 4+中对其进行适当的索引

rails g migration addWinnerReferencesToFights winner_id:references
rails g migration addLooserReferencesToFights looser_id:references

PS-如果你正确拼写模型也会有很大帮助,例如:没有英语单词称为winned或loosed>>>正确的复数是wins和loose(loose表示不紧或不粘贴的东西)

谢谢,这正是问题所在。我在迁移中指定了
t.references:looser,foreign\u key:true,index:true
。我只是不得不从真变假