authentik icon indicating copy to clipboard operation
authentik copied to clipboard

`duplicate key value violates unique constraint` with OAuth Source

Open b- opened this issue 1 year ago • 4 comments

Describe the bug When I log into Authentik by matching a user from an OAuth Source, under certain conditions I get a Python panic with duplicate key value violates unique constraint.

I'm using Tailscale's tsidp as an external OAuth Source, which uses an existing Tailscale connection to authenticate a user, and I'm using the same Tailscale user on multiple Tailscale networks.

Only the first Tailscale network I connect from with a tsidp user logs in seamlessly -- if I connect from another Tailscale network I get a panic. (However, the user is still logged in properly).

To Reproduce Steps to reproduce the behavior:

  1. Sign up for Tailscale and create a Tailscale network
  2. Install a tsidp instance, and set it up as an OAuth Source in Authentik, and confirm it works
  3. Get invited to another Tailscale network as an external user, or create another Tailscale network yourself and send yourself an invite link
  4. Share the tsidp node to the other Tailscale network
  5. Log into Authentik using tsidp from the second networ
  6. Notice the error
  7. Notice we are nevertheless logged in

Expected behavior Authentik should match the user and just log us in, without throwing an error.

Screenshots

Logs

server-1  | {"auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "/", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.asgi", "method": "GET", "pid": 59, "remote": "172.18.0.1", "request_id": "65ff02107e8040819c55cb76e9e12d2f", "runtime": 17, "schema_name": "public", "scheme": "https", "status": 302, "timestamp": "2025-09-14T15:59:37.468545", "user": "", "user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Mobile/15E148 Safari/604.1"}
server-1  | {"auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "/flows/-/default/authentication/?next=/", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.asgi", "method": "GET", "pid": 59, "remote": "172.18.0.1", "request_id": "ed67cbae037649478a25b9d02eecc571", "runtime": 36, "schema_name": "public", "scheme": "https", "status": 302, "timestamp": "2025-09-14T15:59:37.599528", "user": "", "user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Mobile/15E148 Safari/604.1"}
server-1  | {"auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "/if/flow/default-authentication-flow/?next=%2F", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.asgi", "method": "GET", "pid": 59, "remote": "172.18.0.1", "request_id": "a75d96ab3789455f831627ab2a827117", "runtime": 14, "schema_name": "public", "scheme": "https", "status": 200, "timestamp": "2025-09-14T15:59:37.667968", "user": "", "user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Mobile/15E148 Safari/604.1"}
server-1  | {"domain_url": null, "event": "/ws/client/", "level": "info", "logger": "authentik.asgi", "pid": 17770, "remote": "172.18.0.1", "schema_name": "public", "scheme": "ws", "timestamp": "2025-09-14T15:59:37.967842", "user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Mobile/15E148 Safari/604.1"}
server-1  | {"auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "/api/v3/root/config/", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.asgi", "method": "GET", "pid": 59, "remote": "172.18.0.1", "request_id": "f1bb94f395e3496f96aa88faa9bbd8b5", "runtime": 101, "schema_name": "public", "scheme": "https", "status": 200, "timestamp": "2025-09-14T15:59:38.039946", "user": "", "user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Mobile/15E148 Safari/604.1"}
server-1  | {"auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "/api/v3/core/brands/current/", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.asgi", "method": "GET", "pid": 17770, "remote": "172.18.0.1", "request_id": "dfe842dcde03419aab1cbe26607e8c67", "runtime": 177, "schema_name": "public", "scheme": "https", "status": 200, "timestamp": "2025-09-14T15:59:38.136546", "user": "", "user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Mobile/15E148 Safari/604.1"}
server-1  | {"auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "/api/v3/flows/executor/default-authentication-flow/?query=next%3D%252F", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.asgi", "method": "GET", "pid": 59, "remote": "172.18.0.1", "request_id": "ba6e0357b2d04cfe9325c1c4ac6d9ce5", "runtime": 2040, "schema_name": "public", "scheme": "https", "status": 200, "timestamp": "2025-09-14T15:59:39.986811", "user": "", "user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Mobile/15E148 Safari/604.1"}
server-1  | {"auth_via": "unauthenticated", "client_id": "04653f4c84f61eaff0204f3745b3a7fd", "domain_url": "auth.ibeep.com", "event": "redirect args", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.sources.oauth.clients.base", "pid": 17770, "redirect_uri": "https://auth.ibeep.com/source/oauth/callback/tsidp/", "request_id": "e6f5c36bac444120892a954ccb6f0e9c", "response_type": "code", "schema_name": "public", "scope": "email openid profile", "source": "tsidp", "state": "Ew13ilIDZJ6PnHw3vQsUdIwBXZlJWFa4", "timestamp": "2025-09-14T15:59:48.620594"}
server-1  | {"auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "/source/oauth/login/tsidp/", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.asgi", "method": "GET", "pid": 17770, "remote": "172.18.0.1", "request_id": "e6f5c36bac444120892a954ccb6f0e9c", "runtime": 21, "schema_name": "public", "scheme": "https", "status": 302, "timestamp": "2025-09-14T15:59:48.625458", "user": "", "user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Mobile/15E148 Safari/604.1"}
server-1  | {"auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "Task enqueued", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.tasks.middleware", "pid": 17770, "request_id": "dce5750e0d1d4df5bc068d9de2199740", "schema_name": "public", "task_id": "2a5e8622-9e64-4c34-bd35-831e36587630", "task_name": "authentik.events.tasks.event_trigger_dispatch", "timestamp": "2025-09-14T15:59:50.022318"}
server-1  | {"auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "/source/oauth/callback/tsidp/?code=76ed209c01ef43d68320c3f28a531c5b&state=Ew13ilIDZJ6PnHw3vQsUdIwBXZlJWFa4", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.asgi", "method": "GET", "pid": 17770, "remote": "172.18.0.1", "request_id": "dce5750e0d1d4df5bc068d9de2199740", "runtime": 1120, "schema_name": "public", "scheme": "https", "status": 302, "timestamp": "2025-09-14T15:59:50.049174", "user": "", "user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Mobile/15E148 Safari/604.1"}
server-1  | {"auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "/if/flow/default-source-authentication/?code=76ed209c01ef43d68320c3f28a531c5b&state=Ew13ilIDZJ6PnHw3vQsUdIwBXZlJWFa4", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.asgi", "method": "GET", "pid": 17770, "remote": "172.18.0.1", "request_id": "98be03952822439d82e2efe26e28d628", "runtime": 66, "schema_name": "public", "scheme": "https", "status": 200, "timestamp": "2025-09-14T15:59:50.312955", "user": "", "user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Mobile/15E148 Safari/604.1"}
server-1  | {"domain_url": null, "event": "/ws/client/", "level": "info", "logger": "authentik.asgi", "pid": 17770, "remote": "172.18.0.1", "schema_name": "public", "scheme": "ws", "timestamp": "2025-09-14T15:59:50.735400", "user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Mobile/15E148 Safari/604.1"}
server-1  | {"auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "/api/v3/root/config/", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.asgi", "method": "GET", "pid": 17770, "remote": "172.18.0.1", "request_id": "1fa2ae1ec31e471292e78e6b59667698", "runtime": 71, "schema_name": "public", "scheme": "https", "status": 200, "timestamp": "2025-09-14T15:59:50.778768", "user": "", "user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Mobile/15E148 Safari/604.1"}
server-1  | {"auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "/api/v3/core/brands/current/", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.asgi", "method": "GET", "pid": 59, "remote": "172.18.0.1", "request_id": "3b952f59dd094639b5ef3712deb905e6", "runtime": 95, "schema_name": "public", "scheme": "https", "status": 200, "timestamp": "2025-09-14T15:59:50.827346", "user": "", "user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Mobile/15E148 Safari/604.1"}
server-1  | {"auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "discarding key", "host": "auth.ibeep.com", "key": "sub", "level": "info", "logger": "authentik.flows.stage", "pid": 59, "request_id": "e037b48c545e4491b437c1b28cf07dd7", "schema_name": "public", "stage": "update-user-stage", "stage_view": "authentik.stages.user_write.stage.UserWriteStageView", "timestamp": "2025-09-14T15:59:50.849879"}
server-1  | {"auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "Task enqueued", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.tasks.middleware", "pid": 59, "request_id": "e037b48c545e4491b437c1b28cf07dd7", "schema_name": "public", "task_id": "b8930e68-8127-416f-82b3-bd0c85149793", "task_name": "authentik.providers.scim.tasks.scim_sync_direct_dispatch", "timestamp": "2025-09-14T15:59:50.862103"}
server-1  | {"auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "Task enqueued", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.tasks.middleware", "pid": 59, "request_id": "e037b48c545e4491b437c1b28cf07dd7", "schema_name": "public", "task_id": "7595055f-2297-4fa3-9b3c-eb4ffa50b9e9", "task_name": "authentik.enterprise.providers.google_workspace.tasks.google_workspace_sync_direct_dispatch", "timestamp": "2025-09-14T15:59:50.870784"}
server-1  | {"auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "Task enqueued", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.tasks.middleware", "pid": 59, "request_id": "e037b48c545e4491b437c1b28cf07dd7", "schema_name": "public", "task_id": "acbc9d07-9735-4936-9b92-7a52437a6698", "task_name": "authentik.enterprise.providers.microsoft_entra.tasks.microsoft_entra_sync_direct_dispatch", "timestamp": "2025-09-14T15:59:50.879928"}
server-1  | {"action": "model_updated", "auth_via": "unauthenticated", "client_ip": "172.18.0.1", "context": {"http_request": {"args": {"code": "76ed209c01ef43d68320c3f28a531c5b", "state": "Ew13ilIDZJ6PnHw3vQsUdIwBXZlJWFa4"}, "method": "GET", "path": "/api/v3/flows/executor/default-source-authentication/", "request_id": "e037b48c545e4491b437c1b28cf07dd7", "user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Mobile/15E148 Safari/604.1"}, "model": {"app": "authentik_core", "model_name": "user", "name": "b", "pk": 4}}, "domain_url": "auth.ibeep.com", "event": "Created Event", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.events.models", "pid": 59, "request_id": "e037b48c545e4491b437c1b28cf07dd7", "schema_name": "public", "timestamp": "2025-09-14T15:59:50.888792", "user": {"authenticated_as": {"email": "", "is_anonymous": true, "pk": 1, "username": "AnonymousUser"}, "email": "[email protected]", "pk": 4, "username": "b"}}
server-1  | {"auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "Task enqueued", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.tasks.middleware", "pid": 59, "request_id": "e037b48c545e4491b437c1b28cf07dd7", "schema_name": "public", "task_id": "b3d5e548-871c-4596-b732-c67e84955d14", "task_name": "authentik.events.tasks.event_trigger_dispatch", "timestamp": "2025-09-14T15:59:50.897532"}
server-1  | {"action": "user_write", "auth_via": "unauthenticated", "client_ip": "172.18.0.1", "context": {"attributes": {"tsidp_addresses": null, "tsidp_key": "********************", "tsidp_nid": null, "tsidp_node": null, "tsidp_raw_claims": {"email": "[email protected]", "entitlements": ["admin-stuff", "foo"], "groups": ["admin", "member"], "name": "b", "picture": "", "sub": "userid:7385108772371074", "username": "b"}, "tsidp_tags": null, "tsidp_tailnet": null, "tsidp_uid": null, "tsidp_user": null}, "created": false, "email": "[email protected]", "http_request": {"args": {"code": "76ed209c01ef43d68320c3f28a531c5b", "state": "Ew13ilIDZJ6PnHw3vQsUdIwBXZlJWFa4"}, "method": "GET", "path": "/api/v3/flows/executor/default-source-authentication/", "request_id": "e037b48c545e4491b437c1b28cf07dd7", "user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Mobile/15E148 Safari/604.1"}, "name": "b", "path": "goauthentik.io/sources/tsidp", "sub": "[email protected]", "username": "b"}, "domain_url": "auth.ibeep.com", "event": "Created Event", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.events.models", "pid": 59, "request_id": "e037b48c545e4491b437c1b28cf07dd7", "schema_name": "public", "timestamp": "2025-09-14T15:59:50.908924", "user": {"email": "[email protected]", "pk": 4, "username": "b"}}
server-1  | {"auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "Task enqueued", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.tasks.middleware", "pid": 59, "request_id": "e037b48c545e4491b437c1b28cf07dd7", "schema_name": "public", "task_id": "a9ca6bd3-7136-4921-ad99-874cf95ec69d", "task_name": "authentik.events.tasks.event_trigger_dispatch", "timestamp": "2025-09-14T15:59:50.917255"}
server-1  | {"auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "/api/v3/flows/executor/default-source-authentication/?query=code%3D76ed209c01ef43d68320c3f28a531c5b%26state%3DEw13ilIDZJ6PnHw3vQsUdIwBXZlJWFa4", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.asgi", "method": "GET", "pid": 59, "remote": "172.18.0.1", "request_id": "e037b48c545e4491b437c1b28cf07dd7", "runtime": 201, "schema_name": "public", "scheme": "https", "status": 302, "timestamp": "2025-09-14T15:59:50.927576", "user": "", "user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Mobile/15E148 Safari/604.1"}
server-1  | {"action": "login", "auth_via": "unauthenticated", "client_ip": "172.18.0.1", "context": {"http_request": {"args": {"code": "76ed209c01ef43d68320c3f28a531c5b", "state": "Ew13ilIDZJ6PnHw3vQsUdIwBXZlJWFa4"}, "method": "GET", "path": "/api/v3/flows/executor/default-source-authentication/", "request_id": "187bd05917504f26b471a4a5b4567159", "user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Mobile/15E148 Safari/604.1"}, "source": {"app": "authentik_sources_oauth", "model_name": "oauthsource", "name": "Tailscale", "pk": "bdf79e364918498eb1fa81f0de62f311"}}, "domain_url": "auth.ibeep.com", "event": "Created Event", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.events.models", "pid": 59, "request_id": "187bd05917504f26b471a4a5b4567159", "schema_name": "public", "timestamp": "2025-09-14T15:59:51.111096", "user": {"email": "[email protected]", "pk": 4, "username": "b"}}
server-1  | {"auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "Task enqueued", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.tasks.middleware", "pid": 59, "request_id": "187bd05917504f26b471a4a5b4567159", "schema_name": "public", "task_id": "4a993a81-7d6b-436a-9a24-c02ceb92c526", "task_name": "authentik.events.tasks.event_trigger_dispatch", "timestamp": "2025-09-14T15:59:51.150324"}
server-1  | {"amount": 3, "auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "Updated score", "for_ip": "172.18.0.1", "for_user": "b", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.policies.reputation.signals", "pid": 59, "request_id": "187bd05917504f26b471a4a5b4567159", "schema_name": "public", "timestamp": "2025-09-14T15:59:51.218079"}
server-1  | {"auth_via": "unauthenticated", "domain_url": "auth.ibeep.com", "event": "/api/v3/flows/executor/default-source-authentication/?query=code%3D76ed209c01ef43d68320c3f28a531c5b%26state%3DEw13ilIDZJ6PnHw3vQsUdIwBXZlJWFa4", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.asgi", "method": "GET", "pid": 59, "remote": "172.18.0.1", "request_id": "187bd05917504f26b471a4a5b4567159", "runtime": 261, "schema_name": "public", "scheme": "https", "status": 302, "timestamp": "2025-09-14T15:59:51.234907", "user": "b", "user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Mobile/15E148 Safari/604.1"}
server-1  | {"auth_via": "session", "domain_url": "auth.ibeep.com", "event": "IntegrityError('duplicate key value violates unique constraint \"authentik_core_usersourc_user_id_source_id_ad1f5aa7_uniq\"\\nDETAIL:  Key (user_id, source_id)=(4, bdf79e36-4918-498e-b1fa-81f0de62f311) already exists.')", "flow_slug": "default-source-authentication", "host": "auth.ibeep.com", "level": "warning", "logger": "authentik.flows.views.executor", "pid": 59, "request_id": "965f4d8189f942e190e7bf7ddf7da325", "schema_name": "public", "timestamp": "2025-09-14T15:59:51.372811"}
server-1  | {"auth_via": "session", "domain_url": "auth.ibeep.com", "event": "/api/v3/flows/executor/default-source-authentication/?query=code%3D76ed209c01ef43d68320c3f28a531c5b%26state%3DEw13ilIDZJ6PnHw3vQsUdIwBXZlJWFa4", "host": "auth.ibeep.com", "level": "info", "logger": "authentik.asgi", "method": "GET", "pid": 59, "remote": "172.18.0.1", "request_id": "965f4d8189f942e190e7bf7ddf7da325", "runtime": 62, "schema_name": "public", "scheme": "https", "status": 200, "timestamp": "2025-09-14T15:59:51.385761", "user": "b", "user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Mobile/15E148 Safari/604.1"}

Version and Deployment (please complete the following information):

  • authentik version: 2025.8.1
  • Deployment: docker-compose

Additional context Full error (from a different login attempt) with traceback:


duplicate key value violates unique constraint "authentik_core_usersourc_user_id_source_id_ad1f5aa7_uniq" DETAIL: Key (user_id, source_id)=(21, bdf79e36-4918-498e-b1fa-81f0de62f311) already exists.
Traceback (most recent call last):
  File "/authentik/flows/views/executor.py", line 289, in get
    stage_response = self.current_stage_view.dispatch(request)
  File "/authentik/core/sources/stage.py", line 25, in dispatch
    connection.save()
    ~~~~~~~~~~~~~~~^^
  File "/authentik/sources/oauth/models.py", line 325, in save
    super().save(*args, **kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/ak-root/.venv/lib/python3.13/site-packages/django/db/models/base.py", line 892, in save
    self.save_base(
    ~~~~~~~~~~~~~~^
        using=using,
        ^^^^^^^^^^^^
    ...<2 lines>...
        update_fields=update_fields,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/ak-root/.venv/lib/python3.13/site-packages/django/db/models/base.py", line 995, in save_base
    parent_inserted = self._save_parents(
        cls, using, update_fields, force_insert
    )
  File "/ak-root/.venv/lib/python3.13/site-packages/django/db/models/base.py", line 1048, 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.13/site-packages/django/db/models/base.py", line 1161, in _save_table
    results = self._do_insert(
        cls._base_manager, using, fields, returning_fields, raw
    )
  File "/ak-root/.venv/lib/python3.13/site-packages/django/db/models/base.py", line 1202, in _do_insert
    return manager._insert(
           ~~~~~~~~~~~~~~~^
        [self],
        ^^^^^^^
    ...<3 lines>...
        raw=raw,
        ^^^^^^^^
    )
    ^
  File "/ak-root/.venv/lib/python3.13/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.13/site-packages/django/db/models/query.py", line 1847, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/ak-root/.venv/lib/python3.13/site-packages/django/db/models/sql/compiler.py", line 1836, in execute_sql
    cursor.execute(sql, params)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/ak-root/.venv/lib/python3.13/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.13/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.13/site-packages/django/db/backends/utils.py", line 100, in _execute
    with self.db.wrap_database_errors:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/ak-root/.venv/lib/python3.13/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.13/site-packages/django/db/backends/utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/ak-root/.venv/lib/python3.13/site-packages/django_prometheus/db/common.py", line 69, in execute
    return super().execute(*args, **kwargs)
           ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/ak-root/.venv/lib/python3.13/site-packages/psycopg/cursor.py", line 97, 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)=(21, bdf79e36-4918-498e-b1fa-81f0de62f311) already exists.
Request ID

d9dda09f36d44a869109b8ca975da45f

b- avatar Sep 14 '25 16:09 b-

~~hm, this issue seems to go away when using a development version of tsidp built from https://github.com/tailscale/tsidp~~

b- avatar Sep 15 '25 07:09 b-

I thought this issue went away with the rewrite of tsidp, but it turns out that deleting and recreating the OAuth Source seemed to resolve it.

However it's sprung up again, and I'm not sure why.


duplicate key value violates unique constraint "authentik_core_usersourc_user_id_source_id_ad1f5aa7_uniq" DETAIL: Key (user_id, source_id)=(4, e59c9136-346e-42b4-b4a2-78a865832bd6) already exists.
Traceback (most recent call last):
  File "/authentik/flows/views/executor.py", line 289, in get
    stage_response = self.current_stage_view.dispatch(request)
  File "/authentik/core/sources/stage.py", line 25, in dispatch
    connection.save()
    ~~~~~~~~~~~~~~~^^
  File "/authentik/sources/oauth/models.py", line 325, in save
    super().save(*args, **kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/ak-root/.venv/lib/python3.13/site-packages/django/db/models/base.py", line 892, in save
    self.save_base(
    ~~~~~~~~~~~~~~^
        using=using,
        ^^^^^^^^^^^^
    ...<2 lines>...
        update_fields=update_fields,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/ak-root/.venv/lib/python3.13/site-packages/django/db/models/base.py", line 995, in save_base
    parent_inserted = self._save_parents(
        cls, using, update_fields, force_insert
    )
  File "/ak-root/.venv/lib/python3.13/site-packages/django/db/models/base.py", line 1048, 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.13/site-packages/django/db/models/base.py", line 1161, in _save_table
    results = self._do_insert(
        cls._base_manager, using, fields, returning_fields, raw
    )
  File "/ak-root/.venv/lib/python3.13/site-packages/django/db/models/base.py", line 1202, in _do_insert
    return manager._insert(
           ~~~~~~~~~~~~~~~^
        [self],
        ^^^^^^^
    ...<3 lines>...
        raw=raw,
        ^^^^^^^^
    )
    ^
  File "/ak-root/.venv/lib/python3.13/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.13/site-packages/django/db/models/query.py", line 1847, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/ak-root/.venv/lib/python3.13/site-packages/django/db/models/sql/compiler.py", line 1836, in execute_sql
    cursor.execute(sql, params)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/ak-root/.venv/lib/python3.13/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.13/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.13/site-packages/django/db/backends/utils.py", line 100, in _execute
    with self.db.wrap_database_errors:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/ak-root/.venv/lib/python3.13/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.13/site-packages/django/db/backends/utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/ak-root/.venv/lib/python3.13/site-packages/django_prometheus/db/common.py", line 69, in execute
    return super().execute(*args, **kwargs)
           ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/ak-root/.venv/lib/python3.13/site-packages/psycopg/cursor.py", line 97, 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)=(4, e59c9136-346e-42b4-b4a2-78a865832bd6) already exists.
Request ID

ff6024d52ab6403989030aa4c048ac0a

b- avatar Sep 24 '25 21:09 b-

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

edit: I opened a new Issue

katrinm86 avatar May 22 '26 08:05 katrinm86