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

Closed
opened 2025-12-29 21:49:37 +01:00 by adam · 1 comment
Owner

Originally created by @abdulet on GitHub (Oct 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 GitHub (Oct 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) ```
adam added the type: bug label 2025-12-29 21:49:37 +01:00
adam closed this issue 2025-12-29 21:49:37 +01:00
Author
Owner

@abdulet commented on GitHub (Oct 22, 2025):

I've solved the issue by running this bash script (retry.sh at /opt/):

#!/bin/bash

# PATH to manage.py
NETBOX_DIR="/opt/netbox/netbox"

cd "$NETBOX_DIR" || { echo "Can't access to $NETBOX_DIR"; exit 1; }

MAX_RETRIES=100
COUNT=0

while [ $COUNT -lt $MAX_RETRIES ]; do
    echo "Intento $((COUNT+1)) de $MAX_RETRIES..."
    python3 manage.py migrate
    if [ $? -eq 0 ]; then
        echo "Migration done."
        exit 0
    else
        echo "Migration failed. Retrying..."
        COUNT=$((COUNT+1))
        sleep 5  # Waits 5 seconds
    fi
done

echo "Migration failed after $MAX_RETRIES retries."
exit 1

Then execute the script until the migration is done:

source /opt/netbox/venv/bin/activate
cd /opt
chmod +x retry.sh
./retry.sh

Once the migration is done, execute the upgrade.sh script:

source /opt/netbox/venv/bin/activate
cd /opt/netbox/
./upgrade.sh

Hope that this can be useful for others.

@abdulet commented on GitHub (Oct 22, 2025): I've solved the issue by running this bash script (retry.sh at /opt/): ``` #!/bin/bash # PATH to manage.py NETBOX_DIR="/opt/netbox/netbox" cd "$NETBOX_DIR" || { echo "Can't access to $NETBOX_DIR"; exit 1; } MAX_RETRIES=100 COUNT=0 while [ $COUNT -lt $MAX_RETRIES ]; do echo "Intento $((COUNT+1)) de $MAX_RETRIES..." python3 manage.py migrate if [ $? -eq 0 ]; then echo "Migration done." exit 0 else echo "Migration failed. Retrying..." COUNT=$((COUNT+1)) sleep 5 # Waits 5 seconds fi done echo "Migration failed after $MAX_RETRIES retries." exit 1 ``` Then execute the script until the migration is done: ``` source /opt/netbox/venv/bin/activate cd /opt chmod +x retry.sh ./retry.sh ``` Once the migration is done, execute the upgrade.sh script: ``` source /opt/netbox/venv/bin/activate cd /opt/netbox/ ./upgrade.sh ``` Hope that this can be useful for others.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#11764