[英]ActiveRecord::StatementInvalid: PG InFailedSqlTransaction
I am trying to create an ActiveRecord Object.But I'm getting this error while creating it.我正在尝试创建一个 ActiveRecord Object。但是我在创建它时遇到了这个错误。
(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.其他答案都没有解决问题的根本原因。
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
I had this issue.我有这个问题。 Just restart the Rails Server and it should work只需重新启动 Rails 服务器,它应该可以工作
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.我正在使用 database_cleaner gem,并将其配置为如果他们使用 javascript,则不会将测试包装在事务中。 So to solve the issue, I added js: true
to each spec that was causing this problem.所以为了解决这个问题,我在每个导致这个问题的规范中添加了js: true
。 (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). (即使规范实际上并未使用 javascript,这是确保测试不会包含在事务中的最方便的方法。不过,我相信这样做的黑客式方法较少)。
For reference, here is the database_cleaner config from spec/support/database_cleaner.rb
:作为参考,这里是来自spec/support/database_cleaner.rb
的 database_cleaner 配置:
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_fixtures
option is set to true
in spec/spec_helper.rb
.如果您没有使用 database_cleaner,那么测试被包装在事务中的原因可能是use_transactional_fixtures
选项在spec/spec_helper.rb
中设置为true
。 Try setting it to false.尝试将其设置为 false。
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
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.确保您的数据库是最新的,并且您的代码不需要一个不存在的列。
Problem:问题:
Solution:解决方案:
Find incorrect SQL statement and correct it.找出不正确的 SQL 语句并更正它。 If you don't want to correct the SQL statement, use ROLLBACK or RELEASE SAVEPOINT after incorrect SQL statement.如果不想更正 SQL 语句,请在错误的 SQL 语句后使用 ROLLBACK 或 RELEASE SAVEPOINT。
就我而言,我收到此错误只是因为我没有 rake'd 我的测试数据库。
In my case the Postgres configuration at /usr/local/var/postgres/postgresql.conf
had the datetype as the international format of dmy
在我的情况下,/usr/local/ dmy
/usr/local/var/postgres/postgresql.conf
国际格式
Changing datetype to the American format of mdy
fixed this issue for me.将日期类型更改为美国格式的mdy
为我解决了这个问题。
Had similar problem after upgrading Rails from 4.2.2 to 4.2.5 I had to upgrade pg
gem and problem start happening将 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))
Teddy Widom Answer is right in this sense, just to sum the problem:从这个意义上说, Teddy Widom 的答案是正确的,只是总结问题:
Sometimes when you use DatabaseCleaner.clean_with :deletion
you may be interfering PostgreSQL transaction.有时,当您使用DatabaseCleaner.clean_with :deletion
时,您可能会干扰 PostgreSQL 事务。
So solution for me was to replace DatabaseCleaner.clean_with :deletion
in parts of the tests where this was caused with DatabaseCleaner.clean_with :truncation
所以对我来说,解决方案是在由DatabaseCleaner.clean_with :truncation
引起的部分测试中替换DatabaseCleaner.clean_with :deletion
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 ...可能是由这个问题引起的
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 id
It 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)
To troubleshoot this, I ran为了解决这个问题,我跑了
tail -f /usr/local/var/log/postgres.log
Then it is just a matter of replicating the error and watching the output in the log file.然后只需复制错误并查看日志文件中的输出即可。
I had this error while running Rspec.运行 Rspec 时出现此错误。 This happened whithout reason I could perceive as it started after adding 4 messages in a yml file for translations.在 yml 文件中添加 4 条消息以进行翻译后,我无法察觉到这种情况的发生。
Run: rails db:drop db:create db:schema:load RAILS_ENV=test
运行: rails db:drop db:create db:schema:load RAILS_ENV=test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.