Skip to content

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

@mk-fg

Description

@mk-fg

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.

Metadata

Metadata

Assignees

Labels

status: acceptedThis issue has been accepted for implementationtype: bugA confirmed report of unexpected behavior in the application

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions