-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PG::InFailedSqlTransaction after rescuing UniqueConstraintViolation #908
Comments
I'm not able to reproduce here:
The only thing I think that could cause this is is you have some transaction around the entire code, so you are still inside the transaction, in which case the behavior you are seeing is expected. Rescuing exceptions inside transactions and not reraising an exception is a bad idea. Put your rescue block outside of any transaction block. If you can come up with a self-contained, reproducible example that shows the behavior you describe (where it can just be pasted into bin/sequel), please post it and I'll reopen. |
I'm in a spec, and realized it's because config.before :suite do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.around :each do |example|
DatabaseCleaner.cleaning do
example.run
end
end Looks like it supports only |
The simplest solution is using the transaction An alternative solution would be to use a uniqueness validation, which will check for duplicate rows before trying to insert. The auto_validations plugin that ships with Sequel should be able to add one automatically for you. |
Thank you! Removed DatabaseCleaner and used the code from the docs and it works. |
@jeremyevans I have a similar issue in specs. I'm using your transaction-code, to rollback after each spec. It works as you say, but only for the first time. At the second # --- Caused by: ---
# PG::InFailedSqlTransaction:
# ERROR: current transaction is aborted, commands ignored until end of transaction block
# /usr/local/bundle/gems/sequel-5.82.0/lib/sequel/adapters/postgres.rb:171:in `exec' def create
retry_count = 0
begin
result = try_insert
retry_count += 1
end while result.failure? && retry_count < 5
end
def try_insert
Sucess(Model.create(code: gen_new_code))
rescue Sequel::UniqueConstraintViolation
Failure("code is already taken! Retry!")
end
def gen_code
#gen random code
end Do you know why that is? |
You likely need to wrap in a savepoint, as explained above in #908 (comment) If you still need help, please open a new discussion and provide a minimal self contained reproducible example. |
It is already wrapped in an RSpec.configure do |c|
c.around(:each) do |example|
DB.transaction(rollback: :always, auto_savepoint: true){example.run}
end
end |
If you still need help, please open a new discussion and provide a minimal self contained reproducible example. |
Thanks I started a discussion at #2206 |
After rescuing from
Sequel::UniqueConstraintViolation
I'm getting PG::InFailedSqlTransaction on every following query. Bug?The text was updated successfully, but these errors were encountered: