ValueError exception when migrating to v2.11.5 with one or more VRFs defined #4967

Closed
opened 2025-12-29 19:22:44 +01:00 by adam · 1 comment
Owner

Originally created by @jeremystretch on GitHub (Jun 4, 2021).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v2.11.5

Python version

3.7

Steps to Reproduce

  1. Start with NetBox v2.11.4 or earlier
  2. Create a VRF
  3. Create one or more prefixes within that VRF
  4. Upgrade to NetBox v2.11.5

Expected Behavior

The upgrade should complete successfully.

Observed Behavior

A ValueError exception is raised due to a bug in the way VRFs are retrieved in the migration:

ValueError: Cannot query "VRF object (1)": Must be "VRF" instance.

It seems that Django does not like using the "faked" VRF instance returned by apps.get_model() for filtering. It's being cast as a string by filter() rather than its numeric ID beign evaluated.

VRF = apps.get_model('ipam', 'VRF')
...
    # Iterate through all VRFs, rebuilding each
    for vrf in VRF.objects.all():
        rebuild_prefixes(vrf)

Altering the migration to reference the ID directly resolves the issue:

        rebuild_prefixes(vrf.pk)
$ ./manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, circuits, contenttypes, dcim, extras, ipam, secrets, sessions, taggit, tenancy, users, virtualization
Running migrations:
  Applying ipam.0048_prefix_populate_depth_children...
Updating 3157 prefixes...
 OK
Originally created by @jeremystretch on GitHub (Jun 4, 2021). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v2.11.5 ### Python version 3.7 ### Steps to Reproduce 1. Start with NetBox v2.11.4 or earlier 2. Create a VRF 3. Create one or more prefixes within that VRF 4. Upgrade to NetBox v2.11.5 ### Expected Behavior The upgrade should complete successfully. ### Observed Behavior A ValueError exception is raised due to a bug in the way VRFs are retrieved in the migration: ``` ValueError: Cannot query "VRF object (1)": Must be "VRF" instance. ``` It seems that Django does not like using the "faked" VRF instance returned by `apps.get_model()` for filtering. It's being cast as a string by `filter()` rather than its numeric ID beign evaluated. ```python VRF = apps.get_model('ipam', 'VRF') ... # Iterate through all VRFs, rebuilding each for vrf in VRF.objects.all(): rebuild_prefixes(vrf) ``` Altering the migration to reference the ID directly resolves the issue: ```python rebuild_prefixes(vrf.pk) ``` ``` $ ./manage.py migrate Operations to perform: Apply all migrations: admin, auth, circuits, contenttypes, dcim, extras, ipam, secrets, sessions, taggit, tenancy, users, virtualization Running migrations: Applying ipam.0048_prefix_populate_depth_children... Updating 3157 prefixes... OK ```
adam added the type: bugstatus: accepted labels 2025-12-29 19:22:44 +01:00
adam closed this issue 2025-12-29 19:22:44 +01:00
Author
Owner

@maznu commented on GitHub (Jun 5, 2021):

Thanks for fixing this one so quickly, @jeremystretch!

@maznu commented on GitHub (Jun 5, 2021): Thanks for fixing this one so quickly, @jeremystretch!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4967