Repository: wagtail/wagtail

Issue:
Reverse migration for documents 0011 and images 0023 fail
Found a bug? Please fill out the sections below. 👍

### Issue Summary

The reverse migration for documents.migrations.0011 and images.migrations.0023 both fail for the same reason.

These migrations were introduced by #6466 

Here are the stack traces for each:

```
  File "/lib/python3.6/site-packages/wagtail/documents/migrations/0011_add_choose_permissions.py", line 80, in remove_choose_permission_from_collections
    GroupCollectionPermission.objects.filter(permission=choose_permission).delete()
    ...
    'The QuerySet value for an exact lookup must be limited to '
ValueError: The QuerySet value for an exact lookup must be limited to one result using slicing.
```

```
  File "/lib/python3.6/site-packages/wagtail/images/migrations/0023_add_choose_permissions.py", line 80, in remove_choose_permission_from_collections
    GroupCollectionPermission.objects.filter(permission=choose_permission).delete()
    ...
    'The QuerySet value for an exact lookup must be limited to '
ValueError: The QuerySet value for an exact lookup must be limited to one result using slicing.
```

### Steps to Reproduce

1. Install wagtail 2.12.3
2. Migrate forward
3. Run `manage.py migrate documents 0010` or `manage.py migrate images 0022`

* I have confirmed that this issue can be reproduced as described on a fresh Wagtail project: no


### Technical details

* Python version: 3.6.8
* Django version: 2.2.17
* Wagtail version: 2.12.3
* Browser version: N/A



Discussion:

Resolving patch:
diff --git a/wagtail/documents/migrations/0011_add_choose_permissions.py b/wagtail/documents/migrations/0011_add_choose_permissions.py
--- a/wagtail/documents/migrations/0011_add_choose_permissions.py
+++ b/wagtail/documents/migrations/0011_add_choose_permissions.py
@@ -54,7 +54,7 @@ def get_choose_permission(apps):
     return Permission.objects.filter(
         content_type=document_content_type,
         codename__in=['choose_document']
-    )
+    ).first()
 
 
 def copy_choose_permission_to_collections(apps, _schema_editor):
@@ -64,7 +64,8 @@ def copy_choose_permission_to_collections(apps, _schema_editor):
 
     root_collection = Collection.objects.get(depth=1)
 
-    for permission in get_choose_permission(apps):
+    permission = get_choose_permission(apps)
+    if permission:
         for group in Group.objects.filter(permissions=permission):
             GroupCollectionPermission.objects.create(
                 group=group,
@@ -76,8 +77,8 @@ def copy_choose_permission_to_collections(apps, _schema_editor):
 def remove_choose_permission_from_collections(apps, _schema_editor):
     GroupCollectionPermission = apps.get_model('wagtailcore.GroupCollectionPermission')
     choose_permission = get_choose_permission(apps)
-
-    GroupCollectionPermission.objects.filter(permission=choose_permission).delete()
+    if choose_permission:
+        GroupCollectionPermission.objects.filter(permission=choose_permission).delete()
 
 
 class Migration(migrations.Migration):
diff --git a/wagtail/images/migrations/0023_add_choose_permissions.py b/wagtail/images/migrations/0023_add_choose_permissions.py
--- a/wagtail/images/migrations/0023_add_choose_permissions.py
+++ b/wagtail/images/migrations/0023_add_choose_permissions.py
@@ -54,7 +54,7 @@ def get_choose_permission(apps):
     return Permission.objects.filter(
         content_type=image_content_type,
         codename__in=['choose_image']
-    )
+    ).first()
 
 
 def copy_choose_permission_to_collections(apps, _schema_editor):
@@ -64,7 +64,8 @@ def copy_choose_permission_to_collections(apps, _schema_editor):
 
     root_collection = Collection.objects.get(depth=1)
 
-    for permission in get_choose_permission(apps):
+    permission = get_choose_permission(apps)
+    if permission:
         for group in Group.objects.filter(permissions=permission):
             GroupCollectionPermission.objects.create(
                 group=group,
@@ -76,8 +77,8 @@ def copy_choose_permission_to_collections(apps, _schema_editor):
 def remove_choose_permission_from_collections(apps, _schema_editor):
     GroupCollectionPermission = apps.get_model('wagtailcore.GroupCollectionPermission')
     choose_permission = get_choose_permission(apps)
-
-    GroupCollectionPermission.objects.filter(permission=choose_permission).delete()
+    if choose_permission:
+        GroupCollectionPermission.objects.filter(permission=choose_permission).delete()
 
 
 class Migration(migrations.Migration):


Test patch: