arrobalytics / django-ledger

Django Ledger is a double entry accounting system built on the Django Web Framework.
https://www.djangoledger.com
GNU General Public License v3.0
1.03k stars 226 forks source link

RuntimeError: A durable atomic block cannot be nested within another atomic block. #178

Closed simevo closed 11 months ago

simevo commented 12 months ago

Describe the bug

with postgres DB and psycopg 3 driver, occurs randomly when creating entity or vendor etc.:

Request Method: POST
Request URL:    http://localhost:8000/ledger/customer/simevo-pqr0mwbu/create/
Django Version: 4.2.8
Exception Type: RuntimeError
Exception Value:    
A durable atomic block cannot be nested within another atomic block.
Exception Location: /usr/local/lib/python3.11/site-packages/django/db/transaction.py, line 190, in __enter__
Raised during:  django_ledger.views.customer.CustomerModelCreateView
Python Executable:  /usr/local/bin/python3
Python Version: 3.11.6
Python Path:    
['/app',
 '/usr/local/lib/python311.zip',
 '/usr/local/lib/python3.11',
 '/usr/local/lib/python3.11/lib-dynload',
 '/usr/local/lib/python3.11/site-packages']
Server time:    Tue, 05 Dec 2023 13:23:52 +0000

To Reproduce

Expected behavior it should create the entity

Screenshots localhost_8000_ledger_customer_simevo-pqr0mwbu_create_

simevo commented 11 months ago

I think I figured out what is going on here, nothing wrong with your code.

In here I set (as we normally do) ATOMIC_REQUESTS = true as per https://docs.djangoproject.com/en/4.2/topics/db/transactions/#tying-transactions-to-http-requests.

Turns out you already wrap all changes to the DB with transaction.atomic(durable=True): so the two approaches clash together.

I removed the setting:

--- a/django_ledger_starter/settings.py
+++ b/django_ledger_starter/settings.py
@@ -88,7 +88,6 @@ DATABASES = {
         "USER": env("DATABASE_USER"),
         "PASSWORD": env("DATABASE_PASSWORD"),
         "TEST": {"NAME": env("DATABASE_NAME")},
-        "ATOMIC_REQUESTS": True,
     }
 }
 if "DATABASE_HOST" in env:

and the problem goes away. I'll now update https://github.com/arrobalytics/django-ledger-starter/pull/1