我正在try 创建一个ActiveRecord对象.但我在创建它的过程中遇到了这个错误.
(0.1ms) ROLLBACK
ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction: ERROR: current transaction is aborted, commands ignored until end of transaction block
大家对这个问题有什么 idea 吗.
我正在try 创建一个ActiveRecord对象.但我在创建它的过程中遇到了这个错误.
(0.1ms) ROLLBACK
ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction: ERROR: current transaction is aborted, commands ignored until end of transaction block
大家对这个问题有什么 idea 吗.
其他答案都不能解决这个问题.
问题是,当Postgres引发异常时,它会毒害同一连接上的future 交易.
解决方法是回滚有问题的事务:
begin
ActiveRecord...do something...
rescue Exception => e
puts "SQL error in #{ __method__ }"
ActiveRecord::Base.connection.execute 'ROLLBACK'
raise e
end