When upgrading on Python 3, the user is prompted to create new migrations #985

Closed
opened 2025-12-29 16:27:36 +01:00 by adam · 1 comment
Owner

Originally created by @jeremystretch on GitHub (May 24, 2017).

Issue type: Bug report

Python version: 3.x
NetBox version: 2.0.3

When running NetBox on Python 3, applying database migrations will report a warning about unreflected migrations and prompt the user to create new migrations:

root@netbox-test:/opt/netbox/netbox# ./manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, circuits, contenttypes, dcim, extras, ipam, secrets, sessions, tenancy, users
Running migrations:
  No migrations to apply.
  Your models have changes that are not yet reflected in a migration, and so won't be applied.
  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.

This is due to a semantic difference in how Python 2 and Python 3 treat strings differently. This warning can be safely ignored, however a user might understandably proceed with creating new migrations as instructed.

The new migrations will not cause data corruption, however their presence may introduce errors. For example:

$ sudo python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, circuits, contenttypes, dcim, extras, ipam, secrets, sessions, tenancy, users
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/commands/migrate.py", line 163, in handle
    pre_migrate_state = executor._create_project_state(with_applied_migrations=True)
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/executor.py", line 81, in _create_project_state
    migration.mutate_state(state, preserve=False)
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/migration.py", line 92, in mutate_state
    operation.state_forwards(self.app_label, new_state)
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/operations/fields.py", line 199, in state_forwards
    state.models[app_label, self.model_name_lower].fields
KeyError: ('dcim', 'module')

Users are advised to simply delete any migrations they have created (those which are not present in the master branch of the NetBox repository), after which an upgrade can be performed successfully. The next release will include a set of migrations converting all model Python 2 bytestrings to Unicode to ensure users are no longer prompted to create migrations.

Originally created by @jeremystretch on GitHub (May 24, 2017). ### Issue type: Bug report **Python version:** 3.x **NetBox version:** 2.0.3 When running NetBox on Python 3, applying database migrations will report a warning about unreflected migrations and prompt the user to create new migrations: ``` root@netbox-test:/opt/netbox/netbox# ./manage.py migrate Operations to perform: Apply all migrations: admin, auth, circuits, contenttypes, dcim, extras, ipam, secrets, sessions, tenancy, users Running migrations: No migrations to apply. Your models have changes that are not yet reflected in a migration, and so won't be applied. Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them. ``` This is due to a semantic difference in how Python 2 and Python 3 [treat strings differently](https://docs.djangoproject.com/en/1.11/topics/migrations/#supporting-python-2-and-3). This warning can be safely ignored, however a user might understandably proceed with creating new migrations as instructed. The new migrations will not cause data corruption, however their presence may introduce errors. For example: ``` $ sudo python3 manage.py migrate Operations to perform: Apply all migrations: admin, auth, circuits, contenttypes, dcim, extras, ipam, secrets, sessions, tenancy, users Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 363, in execute_from_command_line utility.execute() File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 355, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 283, in run_from_argv self.execute(*args, **cmd_options) File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 330, in execute output = self.handle(*args, **options) File "/usr/local/lib/python3.4/dist-packages/django/core/management/commands/migrate.py", line 163, in handle pre_migrate_state = executor._create_project_state(with_applied_migrations=True) File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/executor.py", line 81, in _create_project_state migration.mutate_state(state, preserve=False) File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/migration.py", line 92, in mutate_state operation.state_forwards(self.app_label, new_state) File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/operations/fields.py", line 199, in state_forwards state.models[app_label, self.model_name_lower].fields KeyError: ('dcim', 'module') ``` Users are advised to simply delete any migrations they have created (those which are not present in the `master` branch of the NetBox repository), after which an upgrade can be performed successfully. The next release will include a set of migrations converting all model Python 2 bytestrings to Unicode to ensure users are no longer prompted to create migrations.
adam added the type: bug label 2025-12-29 16:27:36 +01:00
adam closed this issue 2025-12-29 16:27:36 +01:00
Author
Owner

@jeremystretch commented on GitHub (May 24, 2017):

This issue was addressed in f21c6bca00 and 138cbf9761. Once the new set of migrations has been applied, the warning should no longer appear after future migrations.

@jeremystretch commented on GitHub (May 24, 2017): This issue was addressed in f21c6bca0014d6b04f320efb9c9dd820f2d81b8d and 138cbf9761024a009d315a9a1f2ad9db16b7022a. Once the new set of migrations has been applied, the warning should no longer appear after future migrations.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#985