Upgrade error django.db.utils.IntegrityError: duplicate key value violates unique constraint "auth_permission_pkey" #1000

Closed
opened 2026-04-05 19:35:50 +02:00 by MrUnknownDE · 0 comments
Owner

Originally created by @abdulet on 10/22/2025

NetBox Edition

NetBox Community

NetBox Version

v4.1.7

Python Version

3.10

Steps to Reproduce

It's hard to reproduce as seems something bad in the database.
Simply upgrading from 4.1.7 to 4.2.5, but other server works as expected.

Just adding the bug as reference for others.

Expected Behavior

Upgrade procedure ends properly

Observed Behavior

Exception occurs:
Traceback (most recent call last):
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 105, in _execute
return self.cursor.execute(sql, params)
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django_prometheus/db/common.py", line 69, in execute
return super().execute(*args, **kwargs)
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/psycopg/cursor.py", line 97, in execute
raise ex.with_traceback(None)
psycopg.errors.UniqueViolation: duplicate key value violates unique constraint "auth_permission_pkey"
DETAIL: Key (id)=(620) already exists.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/opt/netbox-4.2.5/netbox/manage.py", line 10, in
execute_from_command_line(sys.argv)
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/core/management/init.py", line 442, in execute_from_command_line
utility.execute()
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/core/management/init.py", line 436, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/core/management/base.py", line 413, in run_from_argv
self.execute(*args, **cmd_options)
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/core/management/base.py", line 459, in execute
output = self.handle(*args, **options)
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/core/management/base.py", line 107, in wrapper
res = handle_func(*args, **kwargs)
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/core/management/commands/migrate.py", line 384, in handle
emit_post_migrate_signal(
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/core/management/sql.py", line 52, in emit_post_migrate_signal
models.signals.post_migrate.send(
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/dispatch/dispatcher.py", line 189, in send
response = receiver(signal=self, sender=sender, **named)
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/contrib/auth/management/init.py", line 105, in create_permissions
Permission.objects.using(using).bulk_create(perms)
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/db/models/query.py", line 835, in bulk_create
returned_columns = self._batched_insert(
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/db/models/query.py", line 1875, in _batched_insert
self._insert(
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/db/models/query.py", line 1847, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1836, in execute_sql
cursor.execute(sql, params)
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 79, in execute
return self._execute_with_wrappers(
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 92, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 100, in _execute
with self.db.wrap_database_errors:
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/db/utils.py", line 91, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 105, in _execute
return self.cursor.execute(sql, params)
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django_prometheus/db/common.py", line 69, in execute
return super().execute(*args, **kwargs)
File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/psycopg/cursor.py", line 97, in execute
raise ex.with_traceback(None)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "auth_permission_pkey"
DETAIL: Key (id)=(620) already exists.

If you run:

source /opt/netbox/venv/bin/activate
python3 /opt/netbox/netbox/manage.py migrate

You can observe that the id increments by 1 in every execution:
DETAIL: Key (id)=(621) already exists.
DETAIL: Key (id)=(622) already exists.
....

Runnin a query in the database you can get the ID that should be in use:

sudo -u postgres psql netbox
netbox=# SELECT * FROM django_migrations_id_seq;
 last_value | log_cnt | is_called
------------+---------+-----------
        698 |      32 | t
(1 row)
*Originally created by @abdulet on 10/22/2025* ### NetBox Edition NetBox Community ### NetBox Version v4.1.7 ### Python Version 3.10 ### Steps to Reproduce It's hard to reproduce as seems something bad in the database. Simply upgrading from 4.1.7 to 4.2.5, but other server works as expected. Just adding the bug as reference for others. ### Expected Behavior Upgrade procedure ends properly ### Observed Behavior Exception occurs: Traceback (most recent call last): File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 105, in _execute return self.cursor.execute(sql, params) File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django_prometheus/db/common.py", line 69, in execute return super().execute(*args, **kwargs) File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/psycopg/cursor.py", line 97, in execute raise ex.with_traceback(None) psycopg.errors.UniqueViolation: duplicate key value violates unique constraint "auth_permission_pkey" DETAIL: Key (id)=(620) already exists. The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/opt/netbox-4.2.5/netbox/manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line utility.execute() File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 436, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/core/management/base.py", line 413, in run_from_argv self.execute(*args, **cmd_options) File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/core/management/base.py", line 459, in execute output = self.handle(*args, **options) File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/core/management/base.py", line 107, in wrapper res = handle_func(*args, **kwargs) File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/core/management/commands/migrate.py", line 384, in handle emit_post_migrate_signal( File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/core/management/sql.py", line 52, in emit_post_migrate_signal models.signals.post_migrate.send( File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/dispatch/dispatcher.py", line 189, in send response = receiver(signal=self, sender=sender, **named) File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/contrib/auth/management/__init__.py", line 105, in create_permissions Permission.objects.using(using).bulk_create(perms) File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/db/models/query.py", line 835, in bulk_create returned_columns = self._batched_insert( File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/db/models/query.py", line 1875, in _batched_insert self._insert( File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/db/models/query.py", line 1847, in _insert return query.get_compiler(using=using).execute_sql(returning_fields) File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1836, in execute_sql cursor.execute(sql, params) File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 79, in execute return self._execute_with_wrappers( File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 92, in _execute_with_wrappers return executor(sql, params, many, context) File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 100, in _execute with self.db.wrap_database_errors: File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/db/utils.py", line 91, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 105, in _execute return self.cursor.execute(sql, params) File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/django_prometheus/db/common.py", line 69, in execute return super().execute(*args, **kwargs) File "/opt/netbox-4.2.5/venv/lib/python3.10/site-packages/psycopg/cursor.py", line 97, in execute raise ex.with_traceback(None) django.db.utils.IntegrityError: duplicate key value violates unique constraint "auth_permission_pkey" DETAIL: Key (id)=(620) already exists. If you run: ``` source /opt/netbox/venv/bin/activate python3 /opt/netbox/netbox/manage.py migrate ``` You can observe that the id increments by 1 in every execution: DETAIL: Key (id)=(621) already exists. DETAIL: Key (id)=(622) already exists. .... Runnin a query in the database you can get the ID that should be in use: ``` sudo -u postgres psql netbox netbox=# SELECT * FROM django_migrations_id_seq; last_value | log_cnt | is_called ------------+---------+----------- 698 | 32 | t (1 row) ```
MrUnknownDE added the type: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bugtype: bug labels 2026-04-05 19:36:08 +02:00
Sign in to join this conversation.
No Label type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug type: bug
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/netbox#1000