ActiveRecord :: StatementInvalid : PG InFailedSqlTransaction
ActiveRecord Object를 생성하려고하는데 생성하는 동안이 오류가 발생합니다.
(0.1ms) ROLLBACK
ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction: ERROR: current transaction is aborted, commands ignored until end of transaction block
문제에 관한 모든 아이디어.
다른 답변으로는 문제의 근본 원인 을 해결할 수 없습니다.
문제는 Postgres가 예외를 발생시킬 때 동일한 연결에서 향후 트랜잭션을 독살한다는 것입니다.
해결 방법은 문제가되는 트랜잭션을 롤백하는 것입니다.
begin
ActiveRecord...do something...
rescue Exception => e
puts "SQL error in #{ __method__ }"
ActiveRecord::Base.connection.execute 'ROLLBACK'
raise e
end
참조를 참조 하십시오 .
나는이 문제가 있었다. Rails 서버를 다시 시작하면 작동합니다.
이 문제는 내 테스트 환경에서 발생했으며 각 테스트가 자체 트랜잭션에 래핑되어 있기 때문에 발생했습니다.
나는 database_cleaner gem을 사용하고 있었고 javascript를 사용하는 경우 트랜잭션에서 테스트를 래핑하지 않도록 구성했습니다. 따라서 문제를 해결하기 js: true
위해이 문제를 일으키는 각 사양을 추가 했습니다. (스펙이 실제로 자바 스크립트를 사용하지 않는다고 생각했지만, 이것은 테스트가 트랜잭션에 래핑되지 않도록하는 가장 편리한 방법입니다. 그래도 해킹 같은 방법이 적다고 확신합니다).
참고로 다음은 다음의 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
당신이 database_cleaner를 사용하지 않는 경우, 다음 아마 테스트가 거래에 싸여 될 이유가 있다는 것이다 use_transactional_fixtures
옵션이 설정되어 true
에서 spec/spec_helper.rb
. 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
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:
- The program executes incorrect SQL statement. Incorrect SQL statement is root cause of the problem.
- The program does not ROLLBACK or RELEASE SAVEPOINT immediately after incorrect SQL statement.
- The program executes SQL statements after incorrect SQL statement.
- PostgreSQL raises ERROR: Current transaction is aborted, commands ignored until end of transaction block
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.
In my case, I received this error simply because I had not rake'd my test db.
In my case the Postgres configuration at /usr/local/var/postgres/postgresql.conf
had the datetype as the international format of dmy
Changing datetype to the American format of mdy
fixed this issue for me.
Had similar problem after upgrading Rails from 4.2.2 to 4.2.5 I had to upgrade pg
gem and problem start happening
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:
Sometimes when you use DatabaseCleaner.clean_with :deletion
you may be interfering PostgreSQL transaction.
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
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
company.timeoffs.where("(start_date <= ? and end_date >= ?) and time_offs.id != ?", begin_date, begin_date, 1)
참고URL : https://stackoverflow.com/questions/21138207/activerecordstatementinvalid-pg-infailedsqltransaction
'IT Share you' 카테고리의 다른 글
테두리가있는 투명한 원 (0) | 2020.11.12 |
---|---|
두 날짜 개체의 시간 차이를 얻는 방법은 무엇입니까? (0) | 2020.11.12 |
장치를 기다리는 Android fastboot (0) | 2020.11.12 |
angularjs : 텍스트 상자에 숫자 만 입력 할 수 있습니다. (0) | 2020.11.12 |
Linux에서 동적으로 로그 파일보기 (0) | 2020.11.12 |