本文介绍了ActiveRecord::StatementInvalid: PG InFailedSqlTransaction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 ActiveRecord 对象.但是我在创建它时遇到了这个错误.

I am trying to create an ActiveRecord Object.But I'm getting this error while creating it.

(0.1ms)  ROLLBACK
ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction: ERROR:  current transaction is       aborted, commands ignored until end of transaction block

关于这个问题的任何想法.

Any ideas folks regarding the issue.

推荐答案

其他答案均无法解决问题的根本原因.

None of the other answers fix the root cause of the issue.

问题在于,当 Postgres 引发异常时,它会毒害同一连接上的未来事务.

The problem is that when Postgres raises an exception, it poisons future transactions on the same connection.

修复方法是回滚违规事务:

The fix is to rollback the offending transaction:

begin
  ActiveRecord...do something...
rescue Exception => e
  puts "SQL error in #{ __method__ }"
  ActiveRecord::Base.connection.execute 'ROLLBACK'

  raise e
end

请参阅参考.

这篇关于ActiveRecord::StatementInvalid: PG InFailedSqlTransaction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 08:37