Opened 6 years ago

Closed 6 years ago

#28920 closed Bug (invalid)

AlterModelManagers migration is generated for all Models with custom Managers, which is not compatible with Django 1.8

Reported by: Michał Bońkowski Owned by: nobody
Component: Migrations Version: 1.11
Severity: Normal Keywords:
Cc: Sjoerd Job Postmus Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Michał Bońkowski)

We have a Model and a default_manager with a custom name:

class MyModel(models.Model):
    my_objects = models.Manager()

This creates a migration with a default manager though no manager is marked with use_in_migrations = True.

class Migration(migrations.Migration):

    operations = [
        migrations.CreateModel(
            name='MyModel',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ],
            options={
                'default_manager_name': 'my_objects',
            },
            managers=[
                ('my_objects', django.db.models.manager.Manager()),
            ],
        ),
    ]

According to https://code.djangoproject.com/ticket/26643#comment:5, we should not get this manager added to the state.

After some investigation, we can see that manager with any other name than objects will be added to the migration: https://github.com/django/django/blob/1.11.8/django/db/migrations/state.py#L537.

Edit:
Under Django 1.8 makemigrations does not create a migration, while in Django 1.11 it creates one.

Change History (6)

comment:1 by Michał Bońkowski, 6 years ago

Description: modified (diff)

comment:2 by Michał Bońkowski, 6 years ago

Description: modified (diff)

comment:3 by Michał Bońkowski, 6 years ago

Component: UncategorizedMigrations
Type: UncategorizedBug

comment:4 by Sjoerd Job Postmus, 6 years ago

Cc: Sjoerd Job Postmus added

comment:5 by Michał Bońkowski, 6 years ago

In our case we resolved it by adding the following condition in migration files:

if VERSION >= (1, 10)
    operations = [
    ...
    ]
else:
    operations = []

comment:6 by Tim Graham, 6 years ago

Resolution: invalid
Status: newclosed

The behavior changed in c0cf73a57d7f77af348aeb854e7b4f670dc3cee7 which suggests the new behavior is desired.

To keep your migrations compatible with older versions of Django, you must generate them with the oldest version of Django that you wish to support, so I see no problem there.

Note: See TracTickets for help on using tickets.
Back to Top