Rails STI和has_many通过关联不起作用,SQLException:没有这样的表 [英] Rails STI and has_many through association not working, SQLException: no such table

查看:93
本文介绍了Rails STI和has_many通过关联不起作用,SQLException:没有这样的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下型号:

class Test < ApplicationRecord
end

class Exam < Test
end

class Practice < Test
  has_many :relations
  has_many :questions, through: :relations
end

class Relation < ApplicationRecord
  belongs_to :practice
  belongs_to :question
end

class Question < ApplicationRecord
  has_many :relations
  has_many :practices, through: :relations
end

这是我的模式:

 create_table "questions", force: :cascade do |t|
    t.string "title"
    t.text "text"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "relations", force: :cascade do |t|
    t.integer "practice_id"
    t.integer "question_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["practice_id"], name: "index_relations_on_practice_id"
    t.index ["question_id"], name: "index_relations_on_question_id"
  end

  create_table "tests", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

当我在Rails控制台中尝试时:

When I try in rails console:

@p = Practice.new
@p.save
@q = Question.new 
@q.save

Practice.last.questions << Question.last

我收到此错误:

Question Load (0.2ms)  SELECT  "questions".* FROM "questions" ORDER BY "questions"."id" DESC LIMIT ?  [["LIMIT", 1]]
   (0.1ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "relations" ("practice_id", "question_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["practice_id", 2], ["question_id", 1], ["created_at", "2017-10-26 06:09:42.581082"], ["updated_at", "2017-10-26 06:09:42.581082"]]
   (0.1ms)  rollback transaction
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.practices: INSERT INTO "relations" ("practice_id", "question_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)

错误很明显,它没有找到表practices,但是我该如何解决呢?我不明白如何指定使用表测试,而不是实践.感谢您的帮助

The error is obvious it doesn't find the table practices but how can I fix this? I don't understand how to specify to use the table tests, instead of practices. Any help is appreciated

推荐答案

不应存在practices表,因为Practice继承了Test并且您要使用STI模式. 我已经在我的机器上重复了您的模型和架​​构,它可以按预期工作.因此,您可能在SQLitespring之类的问题上遇到了问题.

There should be no practices table since Practice inherits Test and you want to use STI pattern. I've repeated your models and schema on my machine and it works as expected. So either you have some problems with SQLite or with things like spring.

使用spring stop冷静一下,然后尝试使用rails db:reset重新创建数据库,并确保没有错误.

Calm down spring with spring stop and then try to recreate db with rails db:reset and make sure there are no errors.

此外,我希望这只是示例代码,在现实生活中,您不要将sql关系命名为relations =)

Moreover I hope it is just example codes and in real life you don't name sql relation as relations =)

这篇关于Rails STI和has_many通过关联不起作用,SQLException:没有这样的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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