Skip to content

[BUG] Cannot delete user (foreign key violation on session) #10384

Description

@p-fruck

Please verify that this bug has NOT been raised before.

  • I checked and didn't find a similar issue

Describe the bug*

As an Admin I want to delete a user but I only get 500 Internal Server Error:

Image

Apparently this is because of a constraint on the session table:

inventree=# select expire_date from user_sessions_session where user_id = 20;
          expire_date          
-------------------------------
 2025-09-22 11:38:03.909674+00
(1 row)

Steps to Reproduce

  1. Register new user
  2. Login with the new user
  3. As an Admin, try to delete the user

Expected behaviour

User should be deleted

Deployment Method

Docker

Version Information

InvenTree-Version: 1.0.1
Django Version: 4.2.24
Commit Hash: 6207aa6
Commit Date: 2025-09-19
Commit Branch: null
Database: django.db.backends.postgresql
Debug-Mode: False
Deployed using Docker: True
Platform: Linux-6.1.0-38-amd64-x86_64-with-glibc2.41
Installer: DOC
Active plugins: false

Try to reproduce on the demo site

I did not try to reproduce

Is the bug reproducible on the demo site?

Not reproducible

Relevant log output

2025-09-23 18:06:29,049 INFO {'request': 'DELETE /api/user/20/', 'user_agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:143.0) Gecko/20100101 Firefox/143.0', 'event': 'request_started', 'ip': '10.140.0.1', 'request_id': 'd914e5a9-0042-49a8-b222-6c96e9c1b9f3', 'user_id': 1, 'timestamp': '2025-09-23T18:06:29.049501Z', 'logger': 'django_structlog.middlewares.request', 'level': 'info'}
2025-09-23T18:06:29.288633Z [error    ] update or delete on table "auth_user" violates foreign key constraint "user_sessions_session_user_id_c6dc9ffa_fk_auth_user_id" on table "user_sessions_session"
DETAIL:  Key (id)=(20) is still referenced from table "user_sessions_session". [inventree] ip=10.140.0.1 request_id=d914e5a9-0042-49a8-b222-6c96e9c1b9f3 user_id=1
2025-09-23 18:06:29,288 ERROR {'event': IntegrityError('update or delete on table "auth_user" violates foreign key constraint "user_sessions_session_user_id_c6dc9ffa_fk_auth_user_id" on table "user_sessions_session"\nDETAIL:  Key (id)=(20) is still referenced from table "user_sessions_session".'), 'ip': '10.140.0.1', 'request_id': 'd914e5a9-0042-49a8-b222-6c96e9c1b9f3', 'user_id': 1, 'timestamp': '2025-09-23T18:06:29.288633Z', 'logger': 'inventree', 'level': 'error'}
2025-09-23T18:06:29.335153Z [info     ] Gathering users for notification 'inventree.error_log' [inventree] ip=10.140.0.1 request_id=d914e5a9-0042-49a8-b222-6c96e9c1b9f3 user_id=1
2025-09-23 18:06:29,335 INFO {'event': "Gathering users for notification 'inventree.error_log'", 'ip': '10.140.0.1', 'request_id': 'd914e5a9-0042-49a8-b222-6c96e9c1b9f3', 'user_id': 1, 'timestamp': '2025-09-23T18:06:29.335153Z', 'logger': 'inventree', 'level': 'info'}
2025-09-23T18:06:29.336341Z [debug    ] Checking plugin registry hash  [inventree] ip=10.140.0.1 request_id=d914e5a9-0042-49a8-b222-6c96e9c1b9f3 user_id=1
2025-09-23 18:06:29,336 DEBUG {'event': 'Checking plugin registry hash', 'ip': '10.140.0.1', 'request_id': 'd914e5a9-0042-49a8-b222-6c96e9c1b9f3', 'user_id': 1, 'timestamp': '2025-09-23T18:06:29.336341Z', 'logger': 'inventree', 'level': 'debug'}
2025-09-23T18:06:29.379639Z [error    ] request_finished               [django_structlog.middlewares.request] code=500 ip=10.140.0.1 request='DELETE /api/user/20/' request_id=d914e5a9-0042-49a8-b222-6c96e9c1b9f3 user_id=1
2025-09-23 18:06:29,379 ERROR {'code': 500, 'request': 'DELETE /api/user/20/', 'event': 'request_finished', 'ip': '10.140.0.1', 'request_id': 'd914e5a9-0042-49a8-b222-6c96e9c1b9f3', 'user_id': 1, 'timestamp': '2025-09-23T18:06:29.379639Z', 'logger': 'django_structlog.middlewares.request', 'level': 'error'}
2025-09-23 18:06:29,380 ERROR Internal Server Error: /api/user/20/
10.140.0.1 - - [23/Sep/2025:18:06:29 +0000] "DELETE /api/user/20/ HTTP/1.1" 500 181 "https://inventree.ctbk.de/web/settings/admin/user/" "Mozilla/5.0 (X11; Linux x86_64; rv:143.0) Gecko/20100101 Firefox/143.0"

Activity

  1. added
    bug
    question
    triage:not-checked
    on Sep 23, 2025
  2. removed
    triage:not-checked
    on Sep 23, 2025
  3. self-assigned this
    on Sep 23, 2025
  4. added theissue type on Sep 23, 2025
  5. removed
    question
    on Sep 23, 2025
  6. added this to the 1.0.2 milestone on Sep 23, 2025
  7. matmair commented on Sep 23, 2025

    @matmair
    Member

    Can reproduce, fix incoming

  8. CobaltDragonGit commented on Sep 24, 2025

    @CobaltDragonGit

    Does this fix also solves the issue of not being able to manually delete access tokens ?

  9. matmair commented on Sep 24, 2025

    @matmair
    Member

    No, only the mentioned things were closed by the PR

  10. CobaltDragonGit commented on Sep 24, 2025

    @CobaltDragonGit

    I had the same issue as p-fruck. I thought the user couldn't be deleted because of the access token so I deleted it manually. I noticed then the table kept the row of the deleted token. Looks like it doesn't seem related.

    I opened a new bug report at #10391

  11. joelidrive commented on Oct 30, 2025

    @joelidrive

    @matmair

    Hello, looks like I am experiencing this same exact issue but I am on version 1.0.8 on a docker deployment. This is the error I receive when I try and delete a specific user.

    update or delete on table "auth_user" violates foreign key constraint "user_sessions_session_user_id_c6dc9ffa_fk_auth_user_id" on table "user_sessions_session"
    DETAIL: Key (id)=(4) is still referenced from table "user_sessions_session".

    InvenTree-Version: 1.0.8
    Django Version: 4.2.25
    Commit Hash: 0dcb706
    Commit Date: 2025-10-21
    Commit Branch: null
    Database: django.db.backends.postgresql
    Debug-Mode: False
    Deployed using Docker: True
    Platform: Linux-6.12.48+deb13-cloud-amd64-x86_64-with-glibc2.41
    Installer: DOC
    Active plugins: [{"name":"InvenTreeBarcode","slug":"inventreebarcode","version":"2.1.0"},{"name":"BOM Exporter","slug":"bom-exporter","version":"1.1.0"},{"name":"InvenTree Exporter","slug":"inventree-exporter","version":"1.0.0"},{"name":"Part Parameter Exporter","slug":"parameter-exporter","version":"1.0.0"},{"name":"InvenTreeEmailNotifications","slug":"inventree-email-notification","version":"1.0.0"},{"name":"InvenTreeUINotifications","slug":"inventree-ui-notification","version":"1.0.0"},{"name":"InvenTreeCurrencyExchange","slug":"inventreecurrencyexchange","version":"1.0.0"},{"name":"InvenTreeMachines","slug":"inventree-machines","version":"1.0.0"},{"name":"InvenTreeLabel","slug":"inventreelabel","version":"1.1.0"},{"name":"InvenTreeLabelMachine","slug":"inventreelabelmachine","version":"1.0.0"},{"name":"InvenTreeLabelSheet","slug":"inventreelabelsheet","version":"1.0.1"},{"name":"DigiKeyBarcodePlugin","slug":"digikeyplugin","version":"1.0.1"},{"name":"LCSCBarcodePlugin","slug":"lcscplugin","version":"1.0.1"},{"name":"MouserBarcodePlugin","slug":"mouserplugin","version":"1.0.1"},{"name":"TMEBarcodePlugin","slug":"tmeplugin","version":"1.0.1"}]

    Image
  12. SchrodingersGat commented on Oct 31, 2025

    @SchrodingersGat
    Member

    I have not been able to reproduce this - @matmair any thoughts?

  13. 18 remaining items

  14. matmair commented on Feb 2, 2026

    @matmair
    Member

    @SchrodingersGat I can not reproduce this and therefore have no way to fix it; unassigning myself

  15. removed their assignment
    on Feb 2, 2026
  16. modified the milestones: 1.2.0, 1.3.0 on Feb 10, 2026
  17. SchrodingersGat commented on Feb 11, 2026

    @SchrodingersGat
    Member

    @p-fruck we are going to need some more information / reproducability before we can fix this I'm afraid

  18. removed this from the 1.3.0 milestone on Mar 21, 2026
  19. github-actions commented on May 20, 2026

    @github-actions
    Contributor

    This issue seems stale. Please react to show this is still important.

  20. added
    inactive
    on May 20, 2026
  21. removed
    inactive
    on May 21, 2026
  22. added
    help wanted
    on Jun 17, 2026
  23. github-actions commented on Aug 17, 2026

    @github-actions
    Contributor

    This issue seems stale. Please react to show this is still important.

  24. added
    inactive
    on Aug 17, 2026
  25. removed
    inactive
    on Aug 17, 2026
  26. SchrodingersGat commented on Aug 21, 2026

    @SchrodingersGat
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    apibughelp wanted

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions