ActiveRecord :: StatementInvalid:PG InFailedSqlTransaction

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

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

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

没有其他答案可以解决问题的根本原因

问题是,当Postgres引发一个exception时,它会在同一个连接上毒害未来的事务。

解决办法是回滚违规事务:

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

参见参考 。

我有这个问题。 只需重新启动Rails服务器,它应该工作

这个问题发生在我的testing环境中,并且是由于每个testing都被封装在自己的事务中。

我正在使用database_cleaner gem,并configuration它,以便不要在事务中包装testing,如果他们使用JavaScript。 所以为了解决这个问题,我在每个引起这个问题的规范中增加了js: true 。 (甚至认为规范没有实际使用javascript,这是确保testing不会被封装在一个事务中的最方便的方式,但我相信这样做的方法还是比较less的)。

作为参考,这里是来自spec/support/database_cleaner.rb的database_cleanerconfiguration:

 RSpec.configure do |config| config.before(:suite) do DatabaseCleaner.clean_with :deletion end config.before(:each) do DatabaseCleaner.strategy = :transaction end config.before(:each, :js => true) do DatabaseCleaner.strategy = :deletion end config.before(:each) do DatabaseCleaner.start end config.after(:each) do DatabaseCleaner.clean end end 

如果你没有使用database_cleaner,那么testing在事务中被包装的原因可能是spec/spec_helper.rb中的use_transactional_fixtures选项被设置为true 。 尝试将其设置为false。

你可以在postgresql日志中看到真正发生了什么,我花了很多时间来深入研究这个问题,最后发现我们误用upsert gem会导致一个PG错误,只有在postgresql日志中才会有真正的信息

https://github.com/seamusabshere/upsert/issues/39

在我的规范中引用一个不再存在的列时,我遇到了这个错误。 确保你的数据库是最新的,你的代码不期望列不存在。

在我的情况下,我收到这个错误只是因为我没有耙我的testing分贝。

在我的情况下, /usr/local/var/postgres/postgresql.conf usr /usr/local/var/postgres/postgresql.conf var /usr/local/var/postgres/postgresql.conf postgres /usr/local/var/postgres/postgresql.conf postgresql.conf中的Postgresconfiguration的datetypes是dmy的国际格式

将datetype更改为美国格式的mdy为我解决了这个问题。

在将Rails从4.2.2升级到4.2.5之后出现了类似的问题,我不得不升级pg gem并且问题开始发生

 9) WorkPolicy#is_publicly_viewable? is publicly visible hides work if deleted Failure/Error: before { DatabaseCleaner.clean_with :deletion } ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction: ERROR: current transaction is aborted, commands ignored until end of transaction block : SELECT tablename FROM pg_tables WHERE schemaname = ANY (current_schemas(false)) 

泰迪Widom答案是正确的,只是为了总结这个问题:

有时候当你使用DatabaseCleaner.clean_with :deletion你可能会干扰PostgreSQL事务。

所以我的解决scheme是取代DatabaseCleaner.clean_with :deletion部分的testing,这是由DatabaseCleaner.clean_with :truncation引起的DatabaseCleaner.clean_with :truncation

Google用户还有一件事。 如果你注意到这个堆栈跟踪:

 An error occurred in an `after(:context)` hook. ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "table_rows" does not exist LINE 1: ...ion_schema.tables WHERE table_schema = 'test' AND table_rows... ^ 

这可能是由这个问题引起的

问题:

  1. 该程序执行不正确的SQL语句。 错误的SQL语句是问题的根本原因。
  2. 在错误的SQL语句之后,程序不立即执行ROLLBACK或RELEASE SAVEPOINT。
  3. 程序在错误的SQL语句之后执行SQL语句。
  4. PostgreSQL引发错误:当前事务被中止,命令被忽略,直到事务块结束

解:

find不正确的SQL语句并更正它。 如果您不想更正SQL语句,请在错误的SQL语句之后使用ROLLBACK或RELEASE SAVEPOINT。

我有这个问题。 我发现这是我的问题。 这意味着当我查询与协会没有指定一个表列。 例如:

 class Holiday < ApplicationRecord belongs_to :company end class Company < ApplicationRecord has_many :timeoffs end 

在假日模型我查询

 company.timeoffs.where("(start_date <= ? and end_date >= ?) and id != ?", begin_date, begin_date, 1) 

发生这个错误是因为我没有指定哪个表的id它改变了我的代码后工作

 company.timeoffs.where("(start_date <= ? and end_date >= ?) and time_offs.id != ?", begin_date, begin_date, 1)