【问题标题】:Rails Rspec - ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction:Rails Rspec - ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction:
【发布时间】:2018-03-08 13:05:14
【问题描述】:

我有这个控制器方法

def dashboard
 method1
 method2
 method3
 ..
end

development 环境中一切正常.. 但在test 环境中, 在最初的几个方法之后我得到了这个错误。

我使用binding.pry 进行了检查,就在我用来出错的行上方,

我无法运行任何查询eg: Account.first 但同样的Account.firstdev 环境中工作

test 环境中运行时出错:

ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction: ERROR:  current transaction is aborted, commands ignored until end of transaction block
: SELECT  "accounts".* FROM "accounts"  ORDER BY "accounts"."id" ASC LIMIT 1
from /home/qwinix/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql_adapter.rb:596:in `async_exec'

请注意以下事项

  • 在其他控制器和相应规范中未发现错误。
  • 其他分支的同一控制器中没有错误
  • 我在method1 中添加了一些代码,在这两种环境下都可以正常执行。我在method3 之后收到这些错误,我还没有碰过。

PS:我用谷歌搜索过stackoverflow问题,没有帮助

编辑:当我将method1 移动到结束时,我没有收到任何错误,请解释一下这种行为。

【问题讨论】:

  • 你能分享你得到错误的实际代码吗?
  • 该消息意味着您在该语句之前 有错误并且没有回滚事务,然后尝试执行您在此处发布的查询并且 postgres 抱怨。您需要启用自动提交并查看哪个查询给出错误,使用 try catch 块或检查您的数据库日志。

标签: ruby-on-rails ruby postgresql activerecord rspec


【解决方案1】:

您是否尝试重置测试数据库?

bundle exec rails db:drop RAILS_ENV=test
bundle exec rails db:create RAILS_ENV=test
bundle exec rails db:schema:load RAILS_ENV=test

或者对于早于 5 的 Rails 版本

bundle exec rake db:drop RAILS_ENV=test
bundle exec rake db:create RAILS_ENV=test
bundle exec rake db:schema:load RAILS_ENV=test

或更短:

bundle exec rake db:reset RAILS_ENV=test

【讨论】:

  • 是的,我确实试过这个bundle exec rake db:drop db:create db:migrate RAILS_ENV=test同样的问题。
猜你喜欢
  • 2014-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-18
  • 2018-06-15
  • 2012-03-13
  • 2015-01-03
  • 2014-02-08
相关资源
最近更新 更多