[Django]-Django migration fails with "__fake__.DoesNotExist: Permission matching query does not exist."

31👍

The default permissions are created in a post_migrate signal handler, after the migrations have run. This won’t be a problem if your updated code runs as part of the second manage.py migrate run, but it is a problem in the test suite and any new deployment.

The easy fix is to change this line:

permission = Permission.objects.get(codename='add_extractionorder',
                                    content_type=content_type) # line 16

to this:

permission, created = Permission.objects.get_or_create(codename='add_extractionorder',
                                              content_type=content_type)

The signal handler that creates the default permissions will never create a duplicate permission, so it is safe to create it if it doesn’t exist already.

👤knbk

Leave a comment