gpt4 book ai didi

ruby-on-rails - 在您的夹具数据中发现外键违规。确保您指的不是关联中不存在的标签

转载 作者:行者123 更新时间:2023-12-02 18:08:00 26 4
gpt4 key购买 nike

我运行一个测试

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

关键部分是:

Foreign key violations found in your fixture data. Ensure you aren't referring to labels that don't exist on associations.

有什么方法可以缩小错误产生的范围吗?

我尝试过的

  • 我试着看看我是否可以在 Rails 控制台中一次加载一个夹具,这样我就可以找出是什么导致了它(但运气不好)
  • 我看到类似的对话 here .

最佳答案

如果您使用的是 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".

https://github.com/rails/rails/blob/v7.0.3/activerecord/lib/active_record/connection_adapters/postgresql/referential_integrity.rb#L41


对于 sqlite3,改为在控制台中运行:

ActiveRecord::Base.connection.execute("PRAGMA foreign_key_check")

# => [{"table"=>"friendships", "rowid"=>1, "parent"=>"users", "fkid"=>0}]

https://github.com/rails/rails/blob/v7.0.3/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb#L214

关于ruby-on-rails - 在您的夹具数据中发现外键违规。确保您指的不是关联中不存在的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72877224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com