Ruby-on-rails ActiveRecord::StatementInvalid: PG InFailedSqlTransaction

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21138207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:14:05  来源:igfitidea点击:

ActiveRecord::StatementInvalid: PG InFailedSqlTransaction

ruby-on-railspostgresqlactiverecordrollbackpg

提问by untwal

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

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

(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.

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

回答by B Seven

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

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

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

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

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

See reference.

参考

回答by Furkan Ayhan

I had this issue. Just restart the Rails Server and it should work

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

回答by Teddy Widom

This issue was occurring in my test environment, and was caused by the fact that each test was wrapped in its own transaction.

这个问题发生在我的测试环境中,是由于每个测试都包含在自己的事务中造成的。

I was using the database_cleaner gem, and have it configured so as NOT to wrap tests in a transaction if they use javascript. So to solve the issue, I added js: trueto each spec that was causing this problem. (Even thought the specs did not actually use javascript, this was the most convenient way to ensure that the tests would not be wrapped in a transaction. I am sure there are less hack-ish ways of doing so, though).

我正在使用 database_cleaner gem,并对其进行了配置,以便在使用 javascript 时不将测试包装在事务中。所以为了解决这个问题,我添加js: true了导致这个问题的每个规范。(即使认为规范实际上并没有使用 javascript,这是确保测试不会包含在事务中的最方便的方法。不过,我确信这样做的黑客方法较少)。

For reference, here is the database_cleaner config from spec/support/database_cleaner.rb:

作为参考,这里是 database_cleaner 配置spec/support/database_cleaner.rb

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

If you are not using database_cleaner, then probably the reason the tests would be wrapped in transactions would be that the use_transactional_fixturesoption is set to truein spec/spec_helper.rb. Try setting it to false.

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

回答by William Herry

you can see what really going on in postgresql log, I spend a lot of time to dig into this issue, and finally find out that we misuse upsert gem cause a PG error, only in postgresql log have the real info of what's going on

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

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

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

回答by lobati

I've run into this error when referencing a column in my specs that no longer exists. Make sure your database is up to date and your code doesn't expect a column that doesn't exist.

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

回答by John Doe

Problem:

问题:

  1. The program executes incorrect SQL statement. Incorrect SQL statement is root cause of the problem.
  2. The program does not ROLLBACK or RELEASE SAVEPOINT immediately after incorrect SQL statement.
  3. The program executes SQL statements after incorrect SQL statement.
  4. PostgreSQL raises ERROR: Current transaction is aborted, commands ignored until end of transaction block
  1. 程序执行了错误的 SQL 语句。不正确的 SQL 语句是问题的根本原因。
  2. 程序不会在错误的 SQL 语句之后立即 ROLLBACK 或 RELEASE SAVEPOINT。
  3. 程序在错误的 SQL 语句之后执行 SQL 语句。
  4. PostgreSQL 引发错误:当前事务已中止,命令被忽略,直到事务块结束

Solution:

解决方案:

Find incorrect SQL statement and correct it. If you don't want to correct the SQL statement, use ROLLBACK or RELEASE SAVEPOINT after incorrect SQL statement.

找出错误的 SQL 语句并改正。如果不想更正 SQL 语句,请在错误的 SQL 语句后使用 ROLLBACK 或 RELEASE SAVEPOINT。

回答by nfriend21

In my case, I received this error simply because I had not rake'd my test db.

就我而言,我收到此错误只是因为我没有 rake'd 我的测试数据库。

回答by Winsor

In my case the Postgres configuration at /usr/local/var/postgres/postgresql.confhad the datetype as the international format of dmy

在我的情况下,Postgres 配置/usr/local/var/postgres/postgresql.conf的日期类型作为国际格式dmy

Changing datetype to the American format of mdyfixed this issue for me.

将日期类型更改为美国格式mdy为我解决了这个问题。

回答by pdkpro

I got that problem. And I found out that it was my query. It mean when I query with association without specifying a table column. ex:

我遇到了那个问题。我发现这是我的查询。这意味着当我在不指定表列的情况下进行关联查询时。前任:

class Holiday < ApplicationRecord
     belongs_to :company
end

class Company < ApplicationRecord
    has_many :timeoffs
end

In Holiday model I query

在假日模型中我查询

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

The error occurs because I didn't specify which table's idIt worked for me after I changed the code to

发生错误是因为我没有指定哪个表id在我将代码更改为后它对我有用

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

回答by equivalent8

Had similar problem after upgrading Rails from 4.2.2 to 4.2.5 I had to upgrade pggem and problem start happening

将 Rails 从 4.2.2 升级到 4.2.5 后遇到类似问题我不得不升级pggem 并且问题开始发生

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))

Teddy Widom Answeris right in this sense, just to sum the problem:

从这个意义上说,Teddy Widom Answer是正确的,只是总结一下问题:

Sometimes when you use DatabaseCleaner.clean_with :deletionyou may be interfering PostgreSQL transaction.

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

So solution for me was to replace DatabaseCleaner.clean_with :deletionin parts of the tests where this was caused with DatabaseCleaner.clean_with :truncation

因此,对我来说,解决方案是DatabaseCleaner.clean_with :deletion在部分测试中替换导致此问题的部分DatabaseCleaner.clean_with :truncation

Just one more thing for googling people. If you are noticing this stack trace:

对于谷歌搜索的人来说,还有一件事。如果您注意到此堆栈跟踪:

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...
^

...it may be caused by this problem

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