10

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?

Shoe
  • 74,840
  • 36
  • 166
  • 272

3 Answers3

19

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

dependencies = [
    ('auth', '__latest__'),
]
Shoe
  • 74,840
  • 36
  • 166
  • 272
  • 5
    In general, though, you don't *want* to reference the latest migration. What if you have a `ForeignKey` to a Model that is later deleted? Your migration will break. It's unlikely to matter with a stable package like `django.contrib.auth` but it's still bad practice and doesn't gain you anything. – Kevin Christopher Henry Sep 07 '14 at 00:37
  • Not at all, that's not how migrations work. Note that `makemigrations` produces dependencies on *specific* migrations, not `__latest__`, and that the Django source code itself references specific migrations in its dependencies (e.g. [here](https://github.com/django/django/blob/master/django/contrib/flatpages/migrations/0001_initial.py)). For more on the problems with using `__latest__` see [here](https://code.djangoproject.com/ticket/23071). – Kevin Christopher Henry Sep 07 '14 at 01:41
  • I suggest that you post a new question, along with your code, explaining the problem you're having. There's not enough to go on here. The point about custom `Managers` is explained in the [documentation](https://docs.djangoproject.com/en/dev/topics/migrations/#historical-models). To make migrations work Django has to have a versioned, *textual* representation of your models, and because "it's impossible to serialize arbitrary Python code" that representation can't include custom methods or `Managers`. – Kevin Christopher Henry Sep 07 '14 at 04:43
3

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

Kevin Christopher Henry
  • 46,175
  • 7
  • 116
  • 102
0

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__")]
smac89
  • 39,374
  • 15
  • 132
  • 179