【发布时间】:2015-09-04 17:00:24
【问题描述】:
我在使用 Minitest 和相关 ActiveRecord 模型(Rails.4.2.3)的固定装置时遇到了问题。
这是两个模型:
# vanguard_fund.rb
class VanguardFund < ActiveRecord::Base
belongs_to :benchmark_fund
...
end
# benchmark_fund.rb
class BenchmarkFund < ActiveRecord::Base
has_many :vanguard_funds
end
相当直接。现在这里是固定装置:
# vanguard_funds.yml
vf1:
name: Vanguard Fund 1
benchmark_fund: bm1
# benchmark_funds.yml
bm1:
name: Benchmark Fund 1
现在我在运行任何测试时收到以下错误:
ERROR["test_#name_returns_the_name_of_the_VanguardFund", BaseTest, 2015-06-08 13:39:28 +0000]
test_#name_returns_the_name_of_the_VanguardFund#BaseTest (1433770768.22s)
ActiveRecord::InvalidForeignKey: ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: insert or update on table "vanguard_funds" violates foreign key constraint "fk_rails_994ab6fe75"
DETAIL: Key (benchmark_fund_id)=(479852872) is not present in table "benchmark_funds".
: INSERT INTO "vanguard_funds" ("name", "created_at", "updated_at", "id", "benchmark_fund_id") VALUES ('Vanguard Fund 1', '2015-09-04 16:48:23', '2015-09-04 16:48:23', 263706224, 479852872)
test_after_commit (0.4.1) lib/test_after_commit.rb:15:in `block in transaction_with_transactional_fixtures'
test_after_commit (0.4.1) lib/test_after_commit.rb:9:in `transaction_with_transactional_fixtures'
有一个基准基金 ID (479852872),但似乎在创建 VanguardFund 时在 BenchmarkFunds 表中找不到该记录??
有什么建议吗?
【问题讨论】:
-
是否在
VanguardFunds 之前加载BenchmarkFunds? -
@muistooshort 我不知道夹具加载是如何工作的,但我认为是这样,因为它按字母顺序排在第一位。事实上,假设 ActiveRecord 比依赖字母顺序来处理这类事情更复杂,所以我对可能发生的事情感到很困惑
标签: ruby-on-rails ruby postgresql fixtures minitest