![](/img/trans.png)
[英]Rails Rspec - ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction:
[英]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
。 (即使規范實際上並未使用 javascript,這是確保測試不會包含在事務中的最方便的方法。不過,我相信這樣做的黑客式方法較少)。
作為參考,這里是來自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
如果您沒有使用 database_cleaner,那么測試被包裝在事務中的原因可能是use_transactional_fixtures
選項在spec/spec_helper.rb
中設置為true
。 嘗試將其設置為 false。
你可以在 postgresql 日志中看到真正發生的事情,我花了很多時間去挖掘這個問題,最后發現我們濫用 upsert gem 導致 PG 錯誤,只有在 postgresql 日志中才有真正的信息。
在我的規范中引用不再存在的列時,我遇到了這個錯誤。 確保您的數據庫是最新的,並且您的代碼不需要一個不存在的列。
問題:
解決方案:
找出不正確的 SQL 語句並更正它。 如果不想更正 SQL 語句,請在錯誤的 SQL 語句后使用 ROLLBACK 或 RELEASE SAVEPOINT。
就我而言,我收到此錯誤只是因為我沒有 rake'd 我的測試數據庫。
在我的情況下,/usr/local/ dmy
/usr/local/var/postgres/postgresql.conf
國際格式
將日期類型更改為美國格式的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))
從這個意義上說, Teddy Widom 的答案是正確的,只是總結問題:
有時,當您使用DatabaseCleaner.clean_with :deletion
時,您可能會干擾 PostgreSQL 事務。
所以對我來說,解決方案是在由DatabaseCleaner.clean_with :truncation
引起的部分測試中替換DatabaseCleaner.clean_with :deletion
谷歌搜索的人還有一件事。 如果您注意到此堆棧跟蹤:
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...
^
...可能是由這個問題引起的
我遇到了那個問題。 我發現這是我的查詢。 這意味着當我在不指定表列的情況下進行關聯查詢時。 前任:
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)
為了解決這個問題,我跑了
tail -f /usr/local/var/log/postgres.log
然后只需復制錯誤並查看日志文件中的輸出即可。
運行 Rspec 時出現此錯誤。 在 yml 文件中添加 4 條消息以進行翻譯后,我無法察覺到這種情況的發生。
運行: rails db:drop db:create db:schema:load RAILS_ENV=test
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.