duplicate key despite form.is_valid()

I get exception reports from a Django app that I created, that I do not understand how it is possible they even get raised:

IntegrityError at /abonnenten/create/
duplicate key value violates unique constraint “abonnenten_abonnent_abonummer_key”
DETAIL: Key (abonummer)=(000056001266) already exists.

My code runs form.save() only after validating the form via form.is_valid() checking before persisting the data, if the PK is available. The only thing that I could imagine would cause this is that another request is being made by another ASGI worker adding this unique key in the exact same time, specifically between the form.is_valid() and form.save() which I find highly unlikely since the app is used by maybe 40 people, of which most use it only 2-3 days a month. But maybe there’s something I’m just not seeing so I wanted to ask in the forum if anyone could point me to somewhere.

This is the trace. I emphasized the relevant part with blank lines.

The above exception (duplicate key value violates unique constraint "abonnenten_abonnent_abonummer_key"
DETAIL:  Key (abonummer)=(000056001266) already exists.) was the direct cause of the following exception:
  File "/srv/myapp/venv/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/srv/myapp/venv/lib/python3.12/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/myapp/venv/lib/python3.12/site-packages/django/contrib/auth/decorators.py", line 59, in _view_wrapper
    return view_func(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^



  File "/srv/myapp/app/abonnenten/views.py", line 369, in abonnent_create_or_update
    abonnent = form.save()
               ^^^^^^^^^^^
  File "/srv/myapp/app/abonnenten/forms.py", line 86, in save
    return super().save(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/myapp/venv/lib/python3.12/site-packages/django/forms/models.py", line 554, in save
    self.instance.save()
    ^^^^^^^^^^^^^^^^^^^^
  File "/srv/myapp/app/abonnenten/models.py", line 319, in save
    super().save(*args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^



  File "/srv/myapp/venv/lib/python3.12/site-packages/django/db/models/base.py", line 902, in save
    self.save_base(
    ^
  File "/srv/myapp/venv/lib/python3.12/site-packages/django/db/models/base.py", line 1008, in save_base
    updated = self._save_table(

  File "/srv/myapp/venv/lib/python3.12/site-packages/django/db/models/base.py", line 1169, in _save_table
    results = self._do_insert(

  File "/srv/myapp/venv/lib/python3.12/site-packages/django/db/models/base.py", line 1210, in _do_insert
    return manager._insert(

  File "/srv/myapp/venv/lib/python3.12/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/myapp/venv/lib/python3.12/site-packages/django/db/models/query.py", line 1868, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/myapp/venv/lib/python3.12/site-packages/django/db/models/sql/compiler.py", line 1882, in execute_sql
    cursor.execute(sql, params)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/myapp/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 79, in execute
    return self._execute_with_wrappers(

  File "/srv/myapp/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 92, in _execute_with_wrappers
    return executor(sql, params, many, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/myapp/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 100, in _execute
    with self.db.wrap_database_errors:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/myapp/venv/lib/python3.12/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/myapp/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/myapp/venv/lib/python3.12/site-packages/psycopg/cursor.py", line 97, in execute
    raise ex.with_traceback(None)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

views.py L368-369:

    if request.method == "POST" and form.is_valid():
        abonnent = form.save()

models.py L317-319:

    def save(self, *args, **kwargs):
        self.adressliste = self.verein.adressliste_set.first()
        super().save(*args, **kwargs)

Please post the complete from, view, and model being saved here.

Sorry, I cannot do that. AFAIK, it does not matter. Is_valid has only two outcomes and only one of these outcomes leads to a save, the other one doesn’t, eg when theres a duplicate key found during the validation within is valid.

If you want help with code, you’ll need to post the code.

If you can’t post this specific code for some reason, then you might want to create a separate minimal example that demonstrates the problem which you can share. This process is often a good way of discovering the error yourself.

If I could reproduce the issue, I would not needed to have asked here.