authentik icon indicating copy to clipboard operation
authentik copied to clipboard

IntegrityError (duplicate key) on connection.save() with OAuth Source

Open katrinm86 opened this issue 6 months ago • 2 comments

Describe the bug

This is a follow up to the (closed) issue: https://github.com/goauthentik/authentik/issues/16754

I can confirm that we see the same issue (connect.save() is called and there you get an IntegrityError as a result) in Version 2026.2.2 when a user logs in through an OAuth Source.

They come through the login from a Device Flow and would be redirected somewhere else. In order to fix it, they have to "get out of the flow" (logout or reopen the page). They seem to get logged in and if they start the flow another time the full login works.

It occurs infrequent enough to be hard to debug well, but it seems that some users get it more often than others. They all come through the same OAuth Source but the Source itself is a "Proxy" that bridges the users from a SAML federation.

How to reproduce

We cannot reliably reproduce it.

Expected behavior

There is no duplicate key error.

Screenshots

No response

Additional context

No response

Deployment Method

Docker

Version

2026.2.2

Relevant log output

Traceback (most recent call last):
  File "/authentik/flows/views/executor.py", line 296, in get
    stage_response = self.current_stage_view.dispatch(request)
  File "/authentik/core/sources/stage.py", line 25, in dispatch
    connection.save()
    ~~~~~~~~~~~~~~~^^
  File "/ak-root/.venv/lib/python3.14/site-packages/django/db/models/base.py", line 902, in save
    self.save_base(
    ~~~~~~~~~~~~~~^
        using=using,
        ^^^^^^^^^^^^
    ...<2 lines>...
        update_fields=update_fields,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/ak-root/.venv/lib/python3.14/site-packages/django/db/models/base.py", line 1005, in save_base
    parent_inserted = self._save_parents(
        cls, using, update_fields, force_insert
    )
  File "/ak-root/.venv/lib/python3.14/site-packages/django/db/models/base.py", line 1058, in _save_parents
    updated = self._save_table(
        cls=parent,
    ...<2 lines>...
        force_insert=parent_inserted or issubclass(parent, force_insert),
    )
  File "/ak-root/.venv/lib/python3.14/site-packages/django/db/models/base.py", line 1169, in _save_table
    results = self._do_insert(
        cls._base_manager, using, fields, returning_fields, raw
    )
  File "/ak-root/.venv/lib/python3.14/site-packages/django/db/models/base.py", line 1210, in _do_insert
    return manager._insert(
           ~~~~~~~~~~~~~~~^
        [self],
        ^^^^^^^
    ...<3 lines>...
        raw=raw,
        ^^^^^^^^
    )
    ^
  File "/ak-root/.venv/lib/python3.14/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/ak-root/.venv/lib/python3.14/site-packages/django/db/models/query.py", line 1873, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/ak-root/.venv/lib/python3.14/site-packages/django/db/models/sql/compiler.py", line 1882, in execute_sql
    cursor.execute(sql, params)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/ak-root/.venv/lib/python3.14/site-packages/sentry_sdk/utils.py", line 1841, in runner
    return sentry_patched_function(*args, **kwargs)
  File "/ak-root/.venv/lib/python3.14/site-packages/sentry_sdk/integrations/django/__init__.py", line 645, in execute
    result = real_execute(self, sql, params)
  File "/ak-root/.venv/lib/python3.14/site-packages/django/db/backends/utils.py", line 79, in execute
    return self._execute_with_wrappers(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        sql, params, many=False, executor=self._execute
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/ak-root/.venv/lib/python3.14/site-packages/django/db/backends/utils.py", line 92, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/ak-root/.venv/lib/python3.14/site-packages/django/db/backends/utils.py", line 100, in _execute
    with self.db.wrap_database_errors:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/ak-root/.venv/lib/python3.14/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/ak-root/.venv/lib/python3.14/site-packages/django/db/backends/utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/ak-root/.venv/lib/python3.14/site-packages/django_prometheus/db/common.py", line 69, in execute
    return super().execute(*args, **kwargs)
           ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/ak-root/.venv/lib/python3.14/site-packages/psycopg/cursor.py", line 117, in execute
    raise ex.with_traceback(None)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "authentik_core_usersourc_user_id_source_id_ad1f5aa7_uniq"
DETAIL:  Key (user_id, source_id)=(XXX, a39163c9-XXXX-XXXX-XXXX-XXXXXXXXXXXX) already exists.

katrinm86 avatar May 22 '26 08:05 katrinm86

Also keeps happening to us. Not sure if this is due to a database issue. During one upgrade in the past we faced issues and had to manually repair things.

maxammann avatar Jul 28 '26 08:07 maxammann

We're closing this in favor of this parent issue: #25331

dominic-r avatar Aug 19 '26 17:08 dominic-r