SQLite3::ConstraintException: FOREIGN KEY constraint failed
Я изучаю Rails и возникла проблема которую я никак не могу решить.
SQLite3::ConstraintException: FOREIGN KEY constraint failed
/db/schema.rb:24:in `block in '
schema.rb
create_table "answers", force: :cascade do |t|
t.string "title", null: false
t.boolean "correct", default: false
t.integer "question_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["question_id"], name: "index_answers_on_question_id"
end
(строка 24) create_table "categories", force: :cascade do |t|
t.string "title", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "questions", force: :cascade do |t|
t.string "title", null: false
t.integer "test_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["test_id"], name: "index_questions_on_test_id"
end
create_table "test_passages", force: :cascade do |t|
t.integer "user_id"
t.integer "test_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["test_id"], name: "index_test_passages_on_test_id"
t.index ["user_id"], name: "index_test_passages_on_user_id"
end
create_table "tests", force: :cascade do |t|
t.string "title", null: false
t.integer "level", default: 0
t.integer "category_id", null: false
t.integer "user_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["category_id"], name: "index_tests_on_category_id"
t.index ["user_id"], name: "index_tests_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "name", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
add_foreign_key "questions", "tests"
add_foreign_key "test_passages", "tests"
add_foreign_key "test_passages", "users"
add_foreign_key "tests", "categories"
add_foreign_key "tests", "users"
end
файл seeds.rb
categories = Category.create!([
{ title: 'RUBY' },
{ title: 'HTML' }
])
users = User.create!([
{ name: '1User' },
{ name: '2User' }
])
tests = Test.create!([
{ level: 1, title: 'RUBY-TEST', category_id: categories.first.id, user_id: users.first.id },
{ level: 2, title: 'HTML-TEST', category_id: categories.last.id, user_id: users.last.id }
])
questions = Question.create!([
{ title: 'Вопрос 1', test_id: tests.first.id },
{ title: 'Вопрос 2', test_id: tests.last.id }
])
answers = Answer.create!([
{ title: 'Ответ', correct: true, question_id: questions.first.id },
{ title: 'НЕ Ответ', correct: false, question_id: questions.first.id },
{ title: 'НЕ Ответ', correct: false, question_id: questions.first.id },
{ title: 'НЕ Ответ', correct: false, question_id: questions.last.id },
{ title: 'Ответ', correct: true, question_id: questions.last.id },
{ title: 'НЕ Ответ', correct: false, question_id: questions.last.id }
])
Проверял модели все has_many содержат dependent: :destroy
Краткий стек-трейс
Caused by:
SQLite3::ConstraintException: FOREIGN KEY constraint failed
C:/Users/qytiz/Desktop/rails/TestGuru/db/schema.rb:23:in `block in <main>'
C:/Users/qytiz/Desktop/rails/TestGuru/db/schema.rb:13:in `<main>'
bin/rails:6:in `<main>'
Tasks: TOP => db:schema:load
полный стек-трейс
Caused by:
SQLite3::ConstraintException: FOREIGN KEY constraint failed
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/sqlite3-1.4.2/lib/sqlite3/resultset.rb:170:in `step'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/sqlite3-1.4.2/lib/sqlite3/resultset.rb:170:in `next_hash'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/sqlite3-1.4.2/lib/sqlite3/resultset.rb:105:in `next'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/sqlite3-1.4.2/lib/sqlite3/resultset.rb:133:in `each'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:202:in `to_a'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:202:in `block in execute'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:151:in `prepare'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:193:in `execute'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/sqlite3/database_statements.rb:25:in `block (2 levels) in execute'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.4/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.4/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.4/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/sqlite3/database_statements.rb:24:in `block in execute'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract_adapter.rb:722:in `block (2 levels) in log'C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.4/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.4/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.4/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.4/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.4/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract_adapter.rb:721:in `block in log'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.4/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract_adapter.rb:712:in `log'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/sqlite3/database_statements.rb:23:in `execute'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/schema_statements.rb:502:in `drop_table'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/schema_statements.rb:312:in `create_table'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/migration.rb:890:in `block in method_missing'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/migration.rb:858:in `block in say_with_time'
C:/Ruby27-x64/lib/ruby/2.7.0/benchmark.rb:293:in `measure'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/migration.rb:858:in `say_with_time'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/migration.rb:879:in `method_missing'
C:/Users/qytiz/Desktop/rails/TestGuru/db/schema.rb:23:in `block in <main>'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/schema.rb:50:in `instance_eval'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/schema.rb:50:in `define'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/schema.rb:46:in `define'
C:/Users/qytiz/Desktop/rails/TestGuru/db/schema.rb:13:in `<main>'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:328:in `load_schema'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:425:in `block in load_schema_current'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:479:in `block (2 levels) in each_current_configuration'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:476:in `each'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:476:in `block in each_current_configuration'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:475:in `each'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:475:in `each_current_configuration'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:424:in `load_schema_current'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/railties/databases.rake:400:in `block (3 levels) in <main>'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task.rb:281:in `block in execute'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task.rb:281:in `each'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task.rb:281:in `execute'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task.rb:199:in `synchronize'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task.rb:199:in `invoke_with_call_chain'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task.rb:188:in `invoke'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.4/lib/active_record/railties/databases.rake:404:in `block (3 levels) in <main>'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task.rb:281:in `block in execute'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task.rb:281:in `each'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task.rb:281:in `execute'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task.rb:199:in `synchronize'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task.rb:199:in `invoke_with_call_chain'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task.rb:243:in `block in invoke_prerequisites'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task.rb:241:in `each'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task.rb:241:in `invoke_prerequisites'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task.rb:218:in `block in invoke_with_call_chain'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task.rb:199:in `synchronize'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task.rb:199:in `invoke_with_call_chain'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task.rb:188:in `invoke'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/application.rb:160:in `invoke_task'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/application.rb:116:in `block (2 levels) in top_level'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/application.rb:116:in `each'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/application.rb:116:in `block in top_level'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/application.rb:125:in `run_with_threads'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/application.rb:110:in `top_level'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/railties-6.0.3.4/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/application.rb:186:in `standard_exception_handling'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/railties-6.0.3.4/lib/rails/commands/rake/rake_command.rb:20:in `perform'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/railties-6.0.3.4/lib/rails/command.rb:48:in `invoke'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/railties-6.0.3.4/lib/rails/commands.rb:18:in `<main>'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:324:in `block in require'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:291:in `load_dependency'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:324:in `require'
bin/rails:6:in `<main>'
Tasks: TOP => db:schema:load
seeds.rb
return if Category.exists?
categories = Category.create!([
{ title: 'RUBY' },
{ title: 'HTML' }
])
users = User.create!([
{ name: '1User' },
{ name: '2User' }
])
tests = Test.create!([
{ level: 1, title: 'RUBY-TEST', category_id: categories.first.id, user_id: users.first.id },
{ level: 2, title: 'HTML-TEST', category_id: categories.last.id, user_id: users.last.id }
])
questions = Question.create!([
{ title: 'Вопрос 1', test_id: tests.first.id },
{ title: 'Вопрос 2', test_id: tests.last.id }
])
answers = Answer.create!([
{ title: 'Ответ', correct: true, question_id: questions.first.id },
{ title: 'НЕ Ответ', correct: false, question_id: questions.first.id },
{ title: 'НЕ Ответ', correct: false, question_id: questions.first.id },
{ title: 'НЕ Ответ', correct: false, question_id: questions.last.id },
{ title: 'Ответ', correct: true, question_id: questions.last.id },
{ title: 'НЕ Ответ', correct: false, question_id: questions.last.id }
])
Все модели:
1.answer.rb
class Answer < ApplicationRecord
belongs_to :question
validates :title, presence: true
validate :num_of_answers
scope :correct_only, -> { where(correct: true) }
private
def num_of_answers
errors.add(:question) if question.answers.count >= 4
end
end
2.application_record.rb
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
3.category.rb
class Category < ApplicationRecord
default_scope { order(title: :asc) }
has_many :tests, dependent: :destroy
validates :title, presence: true
end
4.question.rb
class Question < ApplicationRecord
belongs_to :test
has_many :answers, dependent: :destroy
validates :title, presence: true
end
5.test_passage.rb
class TestPassage < ApplicationRecord
belongs_to :user
belongs_to :test
end
6.test.rb
class Test < ApplicationRecord
belongs_to :author, class_name: 'User', foreign_key: 'user_id'
belongs_to :category
has_many :questions, dependent: :destroy
has_many :test_passeges, dependent: :destroy
has_many :passing_users, dependent: :destroy, through: :test_passeges, source: :user
validates :title, :level, presence: true
validates :level, numericality: { only_integer: true, greater_than: 0 }
validates :title, uniqueness: { scope: :level, message: 'Идентичный тест уже существует' }
scope :easy, -> { where(level: (0..1)) }
scope :medium, -> { where(level: (2..4)) }
scope :hard, -> { where(level: (5..Float::INFINITY)) }
scope :from_category, ->(category) { joins(:category).where(categories: { title: category }).order(title: :desc) }
def self.tests_with_category(category)
Test.from_category(category).pluck(:title)
end
end
7.user.rb
class User < ApplicationRecord
has_many :test_passeges, dependent: :destroy
has_many :tests, dependent: :destroy, through: :test_passeges
validates :name, presence: true
scope :ended_on_level, ->(level) { joins(:test_passeges).where(tests: { level: level }, test_passeges: { user_id: id }) }
end
Ответы (1 шт):
Автор решения: Василиса
→ Ссылка
Выполните rake db:drop перед сетапом базы. Сетап создаёт базу с нуля и загружает в неё схему.