Shoe
Shoe

Reputation: 76308

Specify app dependency in migration

I'm trying to add initial data in Django 1.7 and I've read that it is recommended to use data migrations.

I've created my migration file correctly, called "0001_groups", in which I create few contrib.auth's groups and permissions.

The problem is that it is run before the auth migrations are run.

I went to find out what't the name of the last migration of the auth app, and it's called 0005_alter_user_last_login_null.py. So I tried with:

dependencies = [
    ('auth', '0005_alter_user_last_login_null'),
]

but I get:

KeyError: u"Migration appname.0001_groups dependencies references nonexistent parent node ('auth', '0005_alter_user_last_login_null')"

I've googled that error and it always links to 11 months old fixed bugs of Django.

How can I correctly specify the auth app dependency?

Upvotes: 11

Views: 10711

Answers (3)

smac89
smac89

Reputation: 43264

In my case, I wanted to depend on the very first migration of whatever the django.conf.setting.AUTH_USER_MODEL is set to, so that I don't have to hard-code the app name in my code.

The following will do just that:

dependencies = [migrations.swappable_dependency(settings.AUTH_USER_MODEL)]

which is equivalent to:

dependencies = [(settings.AUTH_USER_MODEL.rsplit(".")[0], "__first__")]

Upvotes: 0

Shoe
Shoe

Reputation: 76308

I've found out that you can reference the last migration with __latest__:

dependencies = [
    ('auth', '__latest__'),
]

Upvotes: 23

Kevin Christopher Henry
Kevin Christopher Henry

Reputation: 49157

You're using 1.7 but looking at the master source tree. See this and try 0001_initial.

Upvotes: 3

Related Questions