I'm trying to create a auth.Group with permissions automaticly with migrations. My problem is that when I run migrate on empty database the migration that tries to attach permission to the group can't find the permission. If I target an earlier migration so that migrate exits without an error the permissions appear into the database and after that the migration code can find the permission. So what can I do so that the migration can reference a permission which is created in an earlier migration when the migrations are run back to back?

def load_data(apps, schema_editor):
    Permission  = apps.get_model('auth', 'Permission')
    Group       = apps.get_model('auth', 'Group')

    can_add = Permission.objects.get(codename='add_game')
    developers = Group.objects.create(name='Developer')

    developers.permissions.add(can_add)
    developers.save()


class Migration(migrations.Migration):

    dependencies = [
        ('myApp', '0004_game'),
    ]

    operations = [
        migrations.RunPython(load_data),
    ]

The game model is created in an earlier migration. This code causes always an error stating that Permission matching query does not exist when I run it with other migrations on an empty database. I'm using python 3.4 with django 1.7.2

  • is add_game permission created in the 0004_game migration? – twil Jan 30 '15 at 1:03
  • It's one of the default permissions that is created for every model (add, change, delete). It is created into the database in 0004_game with the model if the migration targets 0004_game but if all migrations are run in series on an empty database the 0005 migration can't find the permission. – Hannu Huhtanen Jan 30 '15 at 18:28

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.