当我运行bundle exec rake test
时,尝试插入行时出现外键错误:
UserPreferencesControllerTest#test_should_create_user_preference:
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: insert or update on table "user_preferences" violates foreign key constraint "fk_rails_d9451c7b48"
DETAIL: Key (sport_id)=(1) is not present in table "sports".
: INSERT INTO "user_preferences" ("skill_level", "notes", "user_id", "sport_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"
app/controllers/user_preferences_controller.rb:30:in `block in create'
app/controllers/user_preferences_controller.rb:29:in `create'
test/controllers/user_preferences_controller_test.rb:26:in `block (2 levels) in <class:UserPreferencesControllerTest>'
test/controllers/user_preferences_controller_test.rb:25:in `block in <class:UserPreferencesControllerTest>'
看起来没有'体育'装置的id为1。
但是,灯具存在于test / fixtures / sports.yml中,如下所示:
--- {}
basketball:
id: '1'
name: "basketball"
description: "game with a large bouncy ball"
发生了什么事?
答案 0 :(得分:0)
sports.yml的第一行声明运动装置为空集:{}
。将第一行替换为---
而不是--- {}