我运行了一个测试
rake test TEST=test/system/my_test.rb看看这个:
rake test TEST=test/system/my_test.rb
Running 1 tests in a single process (parallelization threshold is 50)
Run options: --seed 48133
# Running:
E
Error:
myTest#test_visiting_the_index:
RuntimeError: Foreign key violations found in your fixture data. Ensure you aren't referring to labels that don't exist on associations.
/Users/st/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/activerecord-7.0.2.4/lib/active_record/fixtures.rb:633:in `block in insert'
/Users/st/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/activerecord-7.0.2.4/lib/active_record/fixtures.rb:621:in `each'
/Users/st/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/activerecord-7.0.2.4/lib/active_record/fixtures.rb:621:in `insert'
/Users/st/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/activerecord-7.0.2.4/lib/active_record/fixtures.rb:607:in `read_and_insert'
/Users/st/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/activerecord-7.0.2.4/lib/active_record/fixtures.rb:567:in `create_fixtures'
/Users/st/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/activerecord-7.0.2.4/lib/active_record/test_fixtures.rb:268:in `load_fixtures'
/Users/st/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/activerecord-7.0.2.4/lib/active_record/test_fixtures.rb:122:in `setup_fixtures'
/Users/st/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/activerecord-7.0.2.4/lib/active_record/test_fixtures.rb:10:in `before_setup'
/Users/st/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/activesupport-7.0.2.4/lib/active_support/testing/setup_and_teardown.rb:40:in `before_setup'
rails test test/system/my_test.rb:12
Finished in 0.190845s, 5.2399 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips关键的部分是:
外键违规发现在您的夹具数据。确保您所指的不是关联中不存在的标签。
有没有办法缩小误差产生的夹具范围?
我试过的
发布于 2022-07-06 07:58:43
如果使用postgres,请检查数据库日志:
ERROR: insert or update on table "friendships" violates foreign key constraint "fk_rails_e3733b59b7"
DETAIL: Key (user_id)=(999) is not present in table "users".你也可以自己检查自己的完整性。您将从夹具中得到一个错误,但是记录应该保留在数据库中。重置以确保没有剩菜。
RAILS_ENV=test bin/rails db:reset
RAILS_ENV=test bin/rails db:fixtures:load
RAILS_ENV=test bin/rails c 在控制台中运行这个命令,您应该得到与上面的日志相同的错误。
ActiveRecord::Base.connection.execute(<<~SQL)
do $$
declare r record;
BEGIN
FOR r IN (
SELECT FORMAT(
'UPDATE pg_constraint SET convalidated=false WHERE conname = ''%I''; ALTER TABLE %I VALIDATE CONSTRAINT %I;',
constraint_name,
table_name,
constraint_name
) AS constraint_check
FROM information_schema.table_constraints WHERE constraint_type = 'FOREIGN KEY'
)
LOOP
EXECUTE (r.constraint_check);
END LOOP;
END;
$$;
SQL
# =>
# PG::ForeignKeyViolation: ERROR: insert or update on table "friendships" violates foreign key constraint "fk_rails_e3733b59b7" (ActiveRecord::InvalidForeignKey)
# DETAIL: Key (user_id)=(999) is not present in table "users".对于sqlite3,请在控制台中运行以下命令:
ActiveRecord::Base.connection.execute("PRAGMA foreign_key_check")
# => [{"table"=>"friendships", "rowid"=>1, "parent"=>"users", "fkid"=>0}]发布于 2022-07-06 12:24:49
因为我有很多夹具,所以我检查了每一个夹具,并确保当它引用一个夹具时,这个被引用的夹具实际上是存在的。为什么?由于一些不相关的原因,我的一些装置是指其他不存在的装置。
下面是一个小例子,假设注释belongs_to一个帖子--这将起作用:
# Posts fixture
one:
title: MyString
content: MyText
# Comment fixture
one:
comment_content: MyString
post: one但这将给出问题的错误:
# Posts fixture
one:
title: MyString
content: MyText
# Comment fixture
one:
comment_content: MyString
post: somethingrandom因此,您必须确保您的“属于”装置指向实际存在的东西(在上面的例子中,有些东西是随机的,不存在,所以它会给出错误)。
更好地理解问题的一个小例子
我发现制作一个最小可复制的应用程序作为例子是非常有帮助的,但我做了以下工作:
rails new testapp
cd testapp
rails g scaffold posts title content:text
rails g scaffold comment comment_content:text post:belongs_to
# Add this to post.rb: has_many :comments, dependent: :destroy
rake db:migrate此时,rails test应该成功,posts.yml应该如下所示:
one:
title: MyString
content: MyText
two:
title: MyString
content: MyTextcomments.yml是这样的:
one:
comment_content: MyText
post: one
two:
comment_content: MyText
post: two现在,您可以摆弄夹具,以复制各种错误。假设我们进入comments.yml并更改注释“one”所属的帖子的名称:
posts.yml:
one:
title: MyString
content: MyText
two:
title: MyString
content: MyTextcomments.yml是这样的:
one:
comment_content: MyText
post: one
two:
comment_content: MyText
post: twwo # <-- typo!对于错误,现在当我们运行测试rake test时,我们得到了错误:
RuntimeError: Foreign key violations found in your fixture data.
Ensure you aren't referring to labels that don't exist on associations.相关问题
从技术上讲,这是一个不同的问题,但是如果您忽略了一个夹具所属的项目,您将得到
Error:
PostsControllerTest#test_should_create_post:
ActiveRecord::NotNullViolation: PG::NotNullViolation:
ERROR: null value in column "post_id" violates not-null constraint(试着在上面的例子中省略post: one )。
为了解决这一问题,系统地处理所有模型文件(及其相应的夹具文件),并确保满足每个belongs_to关联:
belongs_to是否在夹具中都有相应的项。author: one (其中'one‘是作者夹具中一个夹具的名称)。https://stackoverflow.com/questions/72877224
复制相似问题