I am trying to create an ActiveRecord Object.But I\'m getting this error while creating it.
(0.1ms) ROLLBACK
ActiveRecord::StatementInvalid: PG::InFailedSql
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
In my case, I received this error simply because I had not rake'd my test db.
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.
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.
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)