django.db.models.deletion.ProtectedError when upgrading to Netbox 3.4.0 #8777

Closed
opened 2025-12-29 20:41:04 +01:00 by adam · 1 comment
Owner

Originally created by @chris-windscribe on GitHub (Oct 26, 2023).

NetBox version

v3.4.0

Python version

3.8

Steps to Reproduce

When upgrading from netbox 3.3.10 to netbox 3.4.0 I receive the following error after running upgrade.sh:

Collecting static files (python3 netbox/manage.py collectstatic --no-input)...

501 static files copied to '/opt/netbox-3.4.0/netbox/static', 34 unmodified.
Removing stale content types (python3 netbox/manage.py remove_stale_contenttypes --no-input)...
Traceback (most recent call last):
  File "netbox/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/core/management/base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/core/management/base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/contrib/contenttypes/management/commands/remove_stale_contenttypes.py", line 99, in handle
    ct.delete()
  File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/db/models/base.py", line 1117, in delete
    collector.collect([self], keep_parents=keep_parents)
  File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/db/models/deletion.py", line 350, in collect
    raise ProtectedError(
django.db.models.deletion.ProtectedError: ("Cannot delete some instances of model 'ContentType' because they are referenced through protected foreign keys: 'ObjectChange.changed_object_type'.", {<ObjectChange: asn 398962 created by chris>, <ObjectChange: asn 9009 created by chris>, <ObjectChange: asn 51430 created by chris>, <ObjectChange: asn 64515 updated by chris>, <ObjectChange: asn 137409 created by chris>, <ObjectChange: asn 137409 updated by chris>, <ObjectChange: asn 42473 created by chris>, <ObjectChange: asn 25369 created by chris>, <ObjectChange: asn 394256 created by chris>, <ObjectChange: asn 65514 created by chris>, <ObjectChange: asn 132335 created by chris>, <ObjectChange: asn 1234 created by chris>})

As best I can tell there is an object type that has been deprecated which the migration script is attempting to remove, but it cannot due to some existing references.

Expected Behavior

upgrade.sh script should complete sucessfully

Observed Behavior

Collecting static files (python3 netbox/manage.py collectstatic --no-input)...

501 static files copied to '/opt/netbox-3.4.0/netbox/static', 34 unmodified.
Removing stale content types (python3 netbox/manage.py remove_stale_contenttypes --no-input)...
Traceback (most recent call last):
  File "netbox/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/core/management/base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/core/management/base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/contrib/contenttypes/management/commands/remove_stale_contenttypes.py", line 99, in handle
    ct.delete()
  File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/db/models/base.py", line 1117, in delete
    collector.collect([self], keep_parents=keep_parents)
  File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/db/models/deletion.py", line 350, in collect
    raise ProtectedError(
django.db.models.deletion.ProtectedError: ("Cannot delete some instances of model 'ContentType' because they are referenced through protected foreign keys: 'ObjectChange.changed_object_type'.", {<ObjectChange: asn 398962 created by chris>, <ObjectChange: asn 9009 created by chris>, <ObjectChange: asn 51430 created by chris>, <ObjectChange: asn 64515 updated by chris>, <ObjectChange: asn 137409 created by chris>, <ObjectChange: asn 137409 updated by chris>, <ObjectChange: asn 42473 created by chris>, <ObjectChange: asn 25369 created by chris>, <ObjectChange: asn 394256 created by chris>, <ObjectChange: asn 65514 created by chris>, <ObjectChange: asn 132335 created by chris>, <ObjectChange: asn 1234 created by chris>})
Originally created by @chris-windscribe on GitHub (Oct 26, 2023). ### NetBox version v3.4.0 ### Python version 3.8 ### Steps to Reproduce When upgrading from netbox 3.3.10 to netbox 3.4.0 I receive the following error after running `upgrade.sh`: ``` Collecting static files (python3 netbox/manage.py collectstatic --no-input)... 501 static files copied to '/opt/netbox-3.4.0/netbox/static', 34 unmodified. Removing stale content types (python3 netbox/manage.py remove_stale_contenttypes --no-input)... Traceback (most recent call last): File "netbox/manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line utility.execute() File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 440, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/core/management/base.py", line 402, in run_from_argv self.execute(*args, **cmd_options) File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/core/management/base.py", line 448, in execute output = self.handle(*args, **options) File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/contrib/contenttypes/management/commands/remove_stale_contenttypes.py", line 99, in handle ct.delete() File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/db/models/base.py", line 1117, in delete collector.collect([self], keep_parents=keep_parents) File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/db/models/deletion.py", line 350, in collect raise ProtectedError( django.db.models.deletion.ProtectedError: ("Cannot delete some instances of model 'ContentType' because they are referenced through protected foreign keys: 'ObjectChange.changed_object_type'.", {<ObjectChange: asn 398962 created by chris>, <ObjectChange: asn 9009 created by chris>, <ObjectChange: asn 51430 created by chris>, <ObjectChange: asn 64515 updated by chris>, <ObjectChange: asn 137409 created by chris>, <ObjectChange: asn 137409 updated by chris>, <ObjectChange: asn 42473 created by chris>, <ObjectChange: asn 25369 created by chris>, <ObjectChange: asn 394256 created by chris>, <ObjectChange: asn 65514 created by chris>, <ObjectChange: asn 132335 created by chris>, <ObjectChange: asn 1234 created by chris>}) ``` As best I can tell there is an object type that has been deprecated which the migration script is attempting to remove, but it cannot due to some existing references. ### Expected Behavior upgrade.sh script should complete sucessfully ### Observed Behavior ``` Collecting static files (python3 netbox/manage.py collectstatic --no-input)... 501 static files copied to '/opt/netbox-3.4.0/netbox/static', 34 unmodified. Removing stale content types (python3 netbox/manage.py remove_stale_contenttypes --no-input)... Traceback (most recent call last): File "netbox/manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line utility.execute() File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 440, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/core/management/base.py", line 402, in run_from_argv self.execute(*args, **cmd_options) File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/core/management/base.py", line 448, in execute output = self.handle(*args, **options) File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/contrib/contenttypes/management/commands/remove_stale_contenttypes.py", line 99, in handle ct.delete() File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/db/models/base.py", line 1117, in delete collector.collect([self], keep_parents=keep_parents) File "/opt/netbox-3.4.0/venv/lib/python3.8/site-packages/django/db/models/deletion.py", line 350, in collect raise ProtectedError( django.db.models.deletion.ProtectedError: ("Cannot delete some instances of model 'ContentType' because they are referenced through protected foreign keys: 'ObjectChange.changed_object_type'.", {<ObjectChange: asn 398962 created by chris>, <ObjectChange: asn 9009 created by chris>, <ObjectChange: asn 51430 created by chris>, <ObjectChange: asn 64515 updated by chris>, <ObjectChange: asn 137409 created by chris>, <ObjectChange: asn 137409 updated by chris>, <ObjectChange: asn 42473 created by chris>, <ObjectChange: asn 25369 created by chris>, <ObjectChange: asn 394256 created by chris>, <ObjectChange: asn 65514 created by chris>, <ObjectChange: asn 132335 created by chris>, <ObjectChange: asn 1234 created by chris>}) ```
adam closed this issue 2025-12-29 20:41:04 +01:00
Author
Owner

@chris-windscribe commented on GitHub (Oct 27, 2023):

I did some digging and this seems to be an issue with the content in the changelog table. I'm guessing there was a schema change somewhere and the migration script didn't like a few of the entries. After removing them manually using psql I was able to complete the upgrade.sh script.

Example of a couple of 'bad' entries I removed

29031   2022-08-19 15:10:00.367508-04   chris      fc64a9e0-d5b1-4f15-a6bb-ec1363dc24c3    create  6       \N      42473   {"role": null, "site": null, "tags": ["prd"], "group": null, "number": 42473, "status": "active", "ten
ant": null, "created": "2022-08-19", "description": "Anexia Inc", "last_updated": "2022-08-19T19:10:00.359Z", "custom_fields": {}}        113     \N      13      \N


27835   2021-10-18 19:08:51.630195-04   chris   ab22ef37-fb11-44db-a2bb-ed03980ec523    create  1234    \N      31.3.157.240/28 {"vrf": null, "role": 5, "site": 40, "tags": ["prd"], "vlan": null, "prefix": "31.3.157.240/28", "status": "ac
tive", "tenant": 2, "created": "2021-10-18", "is_pool": false, "description": "", "last_updated": "2021-10-18T23:08:51.612Z", "custom_fields": {}, "mark_utilized": false}        48      \N      5       \N
@chris-windscribe commented on GitHub (Oct 27, 2023): I did some digging and this seems to be an issue with the content in the changelog table. I'm guessing there was a schema change somewhere and the migration script didn't like a few of the entries. After removing them manually using `psql` I was able to complete the `upgrade.sh` script. Example of a couple of 'bad' entries I removed ``` 29031 2022-08-19 15:10:00.367508-04 chris fc64a9e0-d5b1-4f15-a6bb-ec1363dc24c3 create 6 \N 42473 {"role": null, "site": null, "tags": ["prd"], "group": null, "number": 42473, "status": "active", "ten ant": null, "created": "2022-08-19", "description": "Anexia Inc", "last_updated": "2022-08-19T19:10:00.359Z", "custom_fields": {}} 113 \N 13 \N 27835 2021-10-18 19:08:51.630195-04 chris ab22ef37-fb11-44db-a2bb-ed03980ec523 create 1234 \N 31.3.157.240/28 {"vrf": null, "role": 5, "site": 40, "tags": ["prd"], "vlan": null, "prefix": "31.3.157.240/28", "status": "ac tive", "tenant": 2, "created": "2021-10-18", "is_pool": false, "description": "", "last_updated": "2021-10-18T23:08:51.612Z", "custom_fields": {}, "mark_utilized": false} 48 \N 5 \N ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#8777