当前位置 : 主页 > 编程语言 > ruby >

ruby-on-rails – Minitest:相关AR模型的装置导致ActiveRecord :: InvalidForeignKey:PG :: F

来源:互联网 收集:自由互联 发布时间:2021-06-23
我遇到了Minitest和相关ActiveRecord模型灯具的问题(Rails.4.2.3). 以下是两种型号: # vanguard_fund.rbclass VanguardFund ActiveRecord::Base belongs_to :benchmark_fund ...end# benchmark_fund.rbclass BenchmarkFund ActiveReco
我遇到了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表中找不到该记录?

有什么建议?

我相信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).

网友评论