Migrating fails from v3.4.7 to v3.5-beta1 #7821

Closed
opened 2025-12-29 20:28:39 +01:00 by adam · 5 comments
Owner

Originally created by @julianstolp on GitHub (Mar 31, 2023).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v3.5-beta1

Python version

3.10

Steps to Reproduce

Upgrading NetBox

cd /opt/netbox
sudo git checkout v3.5-beta1
sudo git pull origin v3.5-beta1
sudo ./upgrade.sh

Wait for the Applying core.0004_replicate_jobresults

Expected Behavior

Upgrade works like usual.

Observed Behavior

Applying database migrations (python3 netbox/manage.py migrate)...
Operations to perform:
  Apply all migrations: admin, auth, circuits, contenttypes, core, dcim, django_rq, extras, ipam, netbox_documents, sessions, social_django, taggit, tenancy, users, virtualization, wireless
Running migrations:
  Applying circuits.0042_provideraccount... OK
  Applying core.0001_initial... OK
  Applying core.0002_managedfile... OK
  Applying core.0003_job... OK
  Applying core.0004_replicate_jobresults...Traceback (most recent call last):
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "core_job_pkey"
DETAIL:  Key (id)=(1) already exists.


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

Traceback (most recent call last):
  File "/opt/netbox/netbox/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/core/management/base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/core/management/base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/core/management/base.py", line 96, in wrapped
    res = handle_func(*args, **kwargs)
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/core/management/commands/migrate.py", line 349, in handle
    post_migrate_state = executor.migrate(
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/migrations/executor.py", line 135, in migrate
    state = self._migrate_all_forwards(
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards
    state = self.apply_migration(
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/migrations/executor.py", line 252, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/migrations/migration.py", line 130, in apply
    operation.database_forwards(
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/migrations/operations/special.py", line 193, in database_forwards
    self.code(from_state.apps, schema_editor)
  File "/opt/netbox/netbox/core/migrations/0004_replicate_jobresults.py", line 31, in replicate_jobresults
    Job.objects.bulk_create(jobs)
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/models/query.py", line 799, in bulk_create
    returned_columns = self._batched_insert(
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/models/query.py", line 1817, in _batched_insert
    self._insert(
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/models/query.py", line 1791, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1660, in execute_sql
    cursor.execute(sql, params)
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute
    with self.db.wrap_database_errors:
  File "/opt/netbox/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/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "core_job_pkey"
DETAIL:  Key (id)=(1) already exists.
Originally created by @julianstolp on GitHub (Mar 31, 2023). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v3.5-beta1 ### Python version 3.10 ### Steps to Reproduce Upgrading NetBox ``` cd /opt/netbox sudo git checkout v3.5-beta1 sudo git pull origin v3.5-beta1 sudo ./upgrade.sh ``` Wait for the `Applying core.0004_replicate_jobresults` ### Expected Behavior Upgrade works like usual. ### Observed Behavior ``` Applying database migrations (python3 netbox/manage.py migrate)... Operations to perform: Apply all migrations: admin, auth, circuits, contenttypes, core, dcim, django_rq, extras, ipam, netbox_documents, sessions, social_django, taggit, tenancy, users, virtualization, wireless Running migrations: Applying circuits.0042_provideraccount... OK Applying core.0001_initial... OK Applying core.0002_managedfile... OK Applying core.0003_job... OK Applying core.0004_replicate_jobresults...Traceback (most recent call last): File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 89, in _execute return self.cursor.execute(sql, params) psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "core_job_pkey" DETAIL: Key (id)=(1) already exists. The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/opt/netbox/netbox/manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/opt/netbox/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line utility.execute() File "/opt/netbox/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/opt/netbox/venv/lib/python3.10/site-packages/django/core/management/base.py", line 402, in run_from_argv self.execute(*args, **cmd_options) File "/opt/netbox/venv/lib/python3.10/site-packages/django/core/management/base.py", line 448, in execute output = self.handle(*args, **options) File "/opt/netbox/venv/lib/python3.10/site-packages/django/core/management/base.py", line 96, in wrapped res = handle_func(*args, **kwargs) File "/opt/netbox/venv/lib/python3.10/site-packages/django/core/management/commands/migrate.py", line 349, in handle post_migrate_state = executor.migrate( File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/migrations/executor.py", line 135, in migrate state = self._migrate_all_forwards( File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards state = self.apply_migration( File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/migrations/executor.py", line 252, in apply_migration state = migration.apply(state, schema_editor) File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/migrations/migration.py", line 130, in apply operation.database_forwards( File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/migrations/operations/special.py", line 193, in database_forwards self.code(from_state.apps, schema_editor) File "/opt/netbox/netbox/core/migrations/0004_replicate_jobresults.py", line 31, in replicate_jobresults Job.objects.bulk_create(jobs) File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/models/manager.py", line 85, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/models/query.py", line 799, in bulk_create returned_columns = self._batched_insert( File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/models/query.py", line 1817, in _batched_insert self._insert( File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/models/query.py", line 1791, in _insert return query.get_compiler(using=using).execute_sql(returning_fields) File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1660, in execute_sql cursor.execute(sql, params) File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 67, in execute return self._execute_with_wrappers( File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers return executor(sql, params, many, context) File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute with self.db.wrap_database_errors: File "/opt/netbox/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/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 89, in _execute return self.cursor.execute(sql, params) django.db.utils.IntegrityError: duplicate key value violates unique constraint "core_job_pkey" DETAIL: Key (id)=(1) already exists. ```
adam added the type: bugstatus: acceptedbeta labels 2025-12-29 20:28:39 +01:00
adam closed this issue 2025-12-29 20:28:39 +01:00
Author
Owner

@PaulR282 commented on GitHub (Mar 31, 2023):

Same problem here

@PaulR282 commented on GitHub (Mar 31, 2023): Same problem here
Author
Owner

@rodvand commented on GitHub (Mar 31, 2023):

Yes, I experienced the same issue. Workaround for me was doing this:
DELETE FROM extras_jobresult; in the postgresql database and running upgrade.sh again.

@rodvand commented on GitHub (Mar 31, 2023): Yes, I experienced the same issue. Workaround for me was doing this: `DELETE FROM extras_jobresult;` in the postgresql database and running `upgrade.sh` again.
Author
Owner

@julianstolp commented on GitHub (Mar 31, 2023):

Can confirm this. I did the following in the admin shell

>>> jobs = JobResult.objects.all()
>>> for job in jobs:
...     job.delete()
...
>>>

Migrating with zero job results works.

@julianstolp commented on GitHub (Mar 31, 2023): Can confirm this. I did the following in the admin shell ``` >>> jobs = JobResult.objects.all() >>> for job in jobs: ... job.delete() ... >>> ``` Migrating with zero job results works.
Author
Owner

@jeremystretch commented on GitHub (Mar 31, 2023):

Whoops, this was a dumb mistake on my part when writing the migration. Existing job results are replicated in batches but the previous batch isn't being cleared from cache after replication.

@jeremystretch commented on GitHub (Mar 31, 2023): Whoops, this was a dumb mistake on my part when writing the migration. Existing job results are replicated in batches but the previous batch isn't being cleared from cache after replication.
Author
Owner

@jeremystretch commented on GitHub (Mar 31, 2023):

I should also note for future reference that this bug is triggered only when there are more than 100 JobResult records in the database.

@jeremystretch commented on GitHub (Mar 31, 2023): I should also note for future reference that this bug is triggered only when there are more than 100 JobResult records in the database.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7821