Django loaddata fails for UserConfig objects due to missing raw=True check in post_save hook #6368

Closed
opened 2025-12-29 19:39:55 +01:00 by adam · 0 comments
Owner

Originally created by @mk-fg on GitHub (Apr 18, 2022).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v3.2.1

Python version

3.9

Steps to Reproduce

  1. Create non-empty UserConfig objects in NetBox database (like one from netbox-community/netbox-demo-data repository), e.g. by selecting UI theme in preferences.
  2. Export NetBox data using "manage.py dumpdata" command.
  3. Import exported data using "manage.py loaddata" command - it should fail.

Expected Behavior

Successful import, regardless of whether non-empty UserConfig exists or not.

Observed Behavior

After UserConfig is created, loaddata no longer works with following exception like following:

nb-postgres-1        | 2022-04-18 11:25:46.806 UTC [880] ERROR:  duplicate key value violates unique constraint "users_userconfig_user_id_key"
nb-postgres-1        | 2022-04-18 11:25:46.806 UTC [880] DETAIL:  Key (user_id)=(2) already exists.
nb-postgres-1        | 2022-04-18 11:25:46.806 UTC [880] STATEMENT:  INSERT INTO "users_userconfig" ("id", "user_id", "data") VALUES (2, 2, '{"ui": {"colormode": "light"}}') RETURNING "users_userconfig"."id"
nb-netbox-1          | 2022-04-18 11:25:47 :: ERROR :: [  0] Error importing data into netbox db: [TransferError] DB-import command failed [code=1]:
nb-netbox-1          |   Failed [00:09.60]: jsonl->db loaddata - [IntegrityError] Problem installing fixture '-': Could not load users.UserConfig(pk=2): duplicate key value violates unique constraint "users_userconfig_user_id_key"
nb-netbox-1          |   DETAIL:  Key (user_id)=(2) already exists.
nb-netbox-1          |   Failed [00:13.22]: import - [IntegrityError] Problem installing fixture '-': Could not load users.UserConfig(pk=2): duplicate key value violates unique constraint "users_userconfig_user_id_key"
nb-netbox-1          |   DETAIL:  Key (user_id)=(2) already exists.
nb-netbox-1          |   Traceback (most recent call last):
nb-netbox-1          |     File "/opt/netbox/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
nb-netbox-1          |       return self.cursor.execute(sql, params)
nb-netbox-1          |   psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "users_userconfig_user_id_key"
nb-netbox-1          |   DETAIL:  Key (user_id)=(2) already exists.

I believe problem is with post_save hook in netbox.users.models.create_userconfig not respecting raw=True keyword and creating UserConfig when User is imported, which creates the conflict with later config for this user from loaded data.

Code fix that seem to work here: 6a221cd975
Please feel free to apply something similar without PR beaureaucracy if possible, as it seem to be a very simple tweak.

Thanks.

Originally created by @mk-fg on GitHub (Apr 18, 2022). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v3.2.1 ### Python version 3.9 ### Steps to Reproduce 1. Create non-empty UserConfig objects in NetBox database (like one from netbox-community/netbox-demo-data repository), e.g. by selecting UI theme in preferences. 2. Export NetBox data using "manage.py dumpdata" command. 3. Import exported data using "manage.py loaddata" command - it should fail. ### Expected Behavior Successful import, regardless of whether non-empty UserConfig exists or not. ### Observed Behavior After UserConfig is created, loaddata no longer works with following exception like following: ``` nb-postgres-1 | 2022-04-18 11:25:46.806 UTC [880] ERROR: duplicate key value violates unique constraint "users_userconfig_user_id_key" nb-postgres-1 | 2022-04-18 11:25:46.806 UTC [880] DETAIL: Key (user_id)=(2) already exists. nb-postgres-1 | 2022-04-18 11:25:46.806 UTC [880] STATEMENT: INSERT INTO "users_userconfig" ("id", "user_id", "data") VALUES (2, 2, '{"ui": {"colormode": "light"}}') RETURNING "users_userconfig"."id" nb-netbox-1 | 2022-04-18 11:25:47 :: ERROR :: [ 0] Error importing data into netbox db: [TransferError] DB-import command failed [code=1]: nb-netbox-1 | Failed [00:09.60]: jsonl->db loaddata - [IntegrityError] Problem installing fixture '-': Could not load users.UserConfig(pk=2): duplicate key value violates unique constraint "users_userconfig_user_id_key" nb-netbox-1 | DETAIL: Key (user_id)=(2) already exists. nb-netbox-1 | Failed [00:13.22]: import - [IntegrityError] Problem installing fixture '-': Could not load users.UserConfig(pk=2): duplicate key value violates unique constraint "users_userconfig_user_id_key" nb-netbox-1 | DETAIL: Key (user_id)=(2) already exists. nb-netbox-1 | Traceback (most recent call last): nb-netbox-1 | File "/opt/netbox/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute nb-netbox-1 | return self.cursor.execute(sql, params) nb-netbox-1 | psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "users_userconfig_user_id_key" nb-netbox-1 | DETAIL: Key (user_id)=(2) already exists. ``` I believe problem is with post_save hook in netbox.users.models.create_userconfig not respecting raw=True keyword and creating UserConfig when User is imported, which creates the conflict with later config for this user from loaded data. Code fix that seem to work here: https://github.com/mk-fg/netbox/commit/6a221cd975452bf9fbab6cf57dc4c3fb4dbca80a Please feel free to apply something similar without PR beaureaucracy if possible, as it seem to be a very simple tweak. Thanks.
adam added the type: bugstatus: accepted labels 2025-12-29 19:39:55 +01:00
adam closed this issue 2025-12-29 19:39:55 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#6368