Order of migration dependencies when running makemigrations from scratch

145 views
Skip to first unread message

Dakota Hawkins

unread,
Dec 7, 2018, 12:51:58 AM12/7/18
to Django users
We haven't really deployed yet, so generally to make migrations we're deleting existing migration files and re-running makemigrations.

We have two apps, and one of them depends on the other as well as django.contrib.auth. In that app's migrations the dependencies often swap order seemingly indeterminately.

migrations.png

The resulting migration includes either:

class Migration(migrations.Migration):
    initial = True
    dependencies = [
        ('auth', '0009_alter_user_last_name_max_length'),
        ('app2', '0001_initial'),
    ]
    ...

or:

class Migration(migrations.Migration):
    initial = True
    dependencies = [
        ('app2', '0001_initial'),
        ('auth', '0009_alter_user_last_name_max_length'),
    ]
    ...

and it seems to switch back and forth with nearly every run.

Does anybody know why, or how to nail down the order? It doesn't seem to make a technical difference, but I'd like to avoid the churn/noise in our repo.

Simon Charette

unread,
Dec 7, 2018, 7:42:09 AM12/7/18
to Django users
Hello Dakota,

Migration.dependencies should really have been defined as a set from the start
as Django doesn't care about their order anyway.

I unfortunately cannot provide a work around but I suggest you submit an
improvement/cleanup ticket to make the order deterministic. It should be
a simple matter of using sorted in MigrationWriter.as_string[0].

Cheers,
Simon

Dakota Hawkins

unread,
Dec 9, 2018, 7:09:42 PM12/9/18
to Django users
Hi Simon!

Thanks for the information, I'll submit a feature request! I dug through the code a bit and the one thing that concerned me a little was the possibility that sorting might have to account for dependencies between your app's dependencies. For example if app2's migration specified that it must run before auth's migration, does that mean app1's migration must appear before auth's or should that be sorted out by a different mechanism?

Thanks,

Dakota

Simon Charette

unread,
Dec 9, 2018, 11:28:54 PM12/9/18
to Django users
Hello Dakota,

From looking at the autodetector code in charge of generating these lists[0]
and the loader code in charge of resolving them[1][2] I'm pretty confident their order
doesn't carry any meaning.

They should have been defined as a set from the beginning.

Cheers,
Simon

Dakota Hawkins

unread,
Dec 10, 2018, 12:52:21 PM12/10/18
to Django users
Thanks again, Simon!

Filed 30029
Reply all
Reply to author
Forward
0 new messages