Rails STI 和 has_many 通过关联不起作用,SQLException:没有这样 table
Rails STI and has_many through association not working, SQLException: no such table
我有以下型号:
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 控制台中尝试时:
@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 (?, ?, ?, ?)
错误很明显,它没有找到 table practices
但我该如何解决这个问题?我不明白如何指定使用 table 测试,而不是实践。感谢任何帮助
应该没有 practices
table 因为 Practice
继承了 Test
并且您想使用 STI 模式。
我已经在我的机器上重复了您的模型和模式,它按预期工作。所以要么你对 SQLite
或 spring
.
有一些问题
用 spring stop
冷静下来 spring 然后尝试用 rails db:reset
重新创建数据库并确保没有错误。
此外,我希望这只是示例代码,在现实生活中你不会将 sql 关系命名为 relations
=)
我有以下型号:
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 控制台中尝试时:
@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 (?, ?, ?, ?)
错误很明显,它没有找到 table practices
但我该如何解决这个问题?我不明白如何指定使用 table 测试,而不是实践。感谢任何帮助
应该没有 practices
table 因为 Practice
继承了 Test
并且您想使用 STI 模式。
我已经在我的机器上重复了您的模型和模式,它按预期工作。所以要么你对 SQLite
或 spring
.
用 spring stop
冷静下来 spring 然后尝试用 rails db:reset
重新创建数据库并确保没有错误。
此外,我希望这只是示例代码,在现实生活中你不会将 sql 关系命名为 relations
=)