ruby-on-rails – Minitest:相关AR模型的装置导致ActiveRecord :: InvalidForeignKey:PG :: ForeignKeyViolation运行测试时

ruby-on-rails – Minitest:相关AR模型的装置导致ActiveRecord :: InvalidForeignKey:PG :: ForeignKeyViolation运行测试时,第1张

概述我遇到了Minitest和相关ActiveRecord模型灯具的问题(Rails.4.2.3). 以下是两种型号: # vanguard_fund.rbclass VanguardFund < ActiveRecord::Base belongs_to :benchmark_fund ...end# benchmark_fund.rbclass BenchmarkFund < A 我遇到了Minitest和相关ActiveRecord模型灯具的问题(Rails.4.2.3).

以下是两种型号:

# vanguard_fund.rbclass VanguardFund < ActiveRecord::Base  belongs_to :benchmark_fund  ...end# benchmark_fund.rbclass BenchmarkFund < ActiveRecord::Base  has_many :vanguard_fundsend

非常直截了当.现在这里是固定装置

# vanguard_funds.ymlvf1:  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',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表中找不到该记录?

有什么建议?

解决方法 我相信Rails会在加载fixture之前尝试禁用参照完整性检查.如果您的Postgresql用户不是超级用户,那么这将无声地失败,随后夹具加载将失败并显示您看到的ActiveRecord :: InvalIDForeignKey错误.

修复是让你的Postgresql用户成为超级用户.使用特权帐户连接到Postgresql,然后发出ALTER USER … WITH SUPERUSER命令.

在Ubuntu上,我这样做了:

su -l postgres -c "psql -c 'alter user jenkins with superuser;'"

在这种情况下,jenkins是未能运行Rails测试的用户.这解决了我的问题.

在edge version of the Rails testing guide中有一个解释(重点补充):

In order to remove existing data from the database,Rails trIEs to disable referential integrity triggers (like foreign keys and check constraints). If you are getting annoying permission errors on running tests,make sure the database user has privilege to disable these triggers in testing environment. (In Postgresql,only superusers can disable all triggers. Read more about Postgresql permissions 07001).

总结

以上是内存溢出为你收集整理的ruby-on-rails – Minitest:相关AR模型的装置导致ActiveRecord :: InvalidForeignKey:PG :: ForeignKeyViolation运行测试时全部内容,希望文章能够帮你解决ruby-on-rails – Minitest:相关AR模型的装置导致ActiveRecord :: InvalidForeignKey:PG :: ForeignKeyViolation运行测试时所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1239031.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存