Data migrations do not use connection provided by the schema editor #11146

Closed
opened 2025-12-29 21:40:57 +01:00 by adam · 0 comments
Owner

Originally created by @jeremystretch on GitHub (May 8, 2025).

Originally assigned to: @jeremystretch on GitHub.

Deployment Type

Self-hosted

NetBox Version

v4.3.0

Python Version

3.10

Steps to Reproduce

This issue affects certain schema migrations within NetBox which manipulate data within the database. Some of the affected migrations are:

  • dcim/0206_load_module_type_profiles
  • ipam/0080_populate_service_parents
  • tenancy/0018_contact_group

The migrations may not function correctly if applied to a non-default database (i.e. manage.py migrate --database foo). This hasn't surfaced as an issue to date, but presents a concern now that support for external databases has been added in NetBox v4.3.0 under #18780.

Expected Behavior

ORM queries made within the context of a migration should always explicitly reference the connection provided by the schema editor. For example:

def migrate_contact_groups(apps, schema_editor):
    Contact = apps.get_model('tenancy', 'Contact')
    Contact.objects.using(schema_editor.connection.alias).update(...)

Observed Behavior

These queries are being executed without specifying a connection, which results in utilizing the default database.

def migrate_contact_groups(apps, schema_editor):
    Contact = apps.get_model('tenancy', 'Contact')
    Contact.objects.update(...)
Originally created by @jeremystretch on GitHub (May 8, 2025). Originally assigned to: @jeremystretch on GitHub. ### Deployment Type Self-hosted ### NetBox Version v4.3.0 ### Python Version 3.10 ### Steps to Reproduce This issue affects certain schema migrations within NetBox which manipulate data within the database. Some of the affected migrations are: - `dcim/0206_load_module_type_profiles` - `ipam/0080_populate_service_parents` - `tenancy/0018_contact_group` The migrations may not function correctly if applied to a non-default database (i.e. `manage.py migrate --database foo`). This hasn't surfaced as an issue to date, but presents a concern now that support for external databases has been added in NetBox v4.3.0 under #18780. ### Expected Behavior ORM queries made within the context of a migration should always explicitly reference the connection provided by the schema editor. For example: ```python def migrate_contact_groups(apps, schema_editor): Contact = apps.get_model('tenancy', 'Contact') Contact.objects.using(schema_editor.connection.alias).update(...) ``` ### Observed Behavior These queries are being executed without specifying a connection, which results in utilizing the default database. ```python def migrate_contact_groups(apps, schema_editor): Contact = apps.get_model('tenancy', 'Contact') Contact.objects.update(...) ```
adam added the type: bugstatus: acceptedseverity: medium labels 2025-12-29 21:40:57 +01:00
adam closed this issue 2025-12-29 21:40:57 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#11146