简体   繁体   English

后挂钩错误,来自Rspec的PG :: InFailedSqlTransaction

[英]Error in an after hook, PG::InFailedSqlTransaction from Rspec

I was trying to run rspec from a model spec file, but I got this error: "An error occurred in an after hook" 我试图从模型spec文件运行rspec,但我收到此错误:“挂钩后发生错误”

"An error occurred in to after hook PG :: InFailedSqlTransaction: ERROR: current transaction is aborted, commands ignored until end of transaction. occurred at C :/ Ruby193/lib/.../postgresql_adapter: 294 " “挂钩后发生错误PG :: InFailedSqlTransaction:错误:当前事务中止,命令被忽略,直到事务结束。发生在C:/ Ruby193 / lib /.../ postgresql_adapter:294”

I googled this issue, and I found a suggestion to downgrade my 'database_cleaner' to '1.0.1'. 我搜索了这个问题,我发现了将'database_cleaner'降级为'1.0.1'的建议。 I did, but it doesn't work. 我做了,但它不起作用。 Does anyone have any idea how to solve this? 有谁知道如何解决这个问题? Thanks in advance! 提前致谢!

This can happen if you execute a bad SQL statement in the scope of a transaction, you rescue the exception from that statement, and then try and execute another SQL statement in the same transaction. 如果在事务范围内执行错误的SQL语句,从该语句中抢救异常,然后尝试在同一事务中执行另一个SQL语句,则会发生这种情况。

Once one statement in a transaction fails no more statements can be executed in that transaction. 一旦事务中的一个语句失败,就不能再在该事务中执行语句。

Here's an example: 这是一个例子:

ActiveRecord::Base.transaction do
  begin
    ActiveRecord::Base.connection.execute "A bad query"
  rescue => ex 
    puts ex.message
  end
  puts User.count
end

User.count raises PG::InFailedSqlTransaction because the previous SQL statement raised ActiveRecord::StatementInvalid and that was swallowed by the rescue . User.count引发PG::InFailedSqlTransaction因为之前的SQL语句引发了ActiveRecord::StatementInvalid并且被rescue吞没了。

So I would look for code that rescues in the scope of a transaction and then tries to run additional SQL statements. 所以我会寻找在事务范围内挽救的代码,然后尝试运行其他SQL语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM