# Generated by Django 2.2.17 on 2021-01-18 01:48

from django.db import migrations
from django.db.models import Count
from django.db.models.query import QuerySet
import itertools

def do_delete(model, field_name):
    dupes = model.objects.values_list('owner', field_name).annotate(count=Count('pk')).filter(count__gt=1).values('owner', field_name)
    to_delete = model.objects.none()

    for d in dupes.iterator(chunk_size=5000):
        to_delete |= model.objects.filter(**d)[1:].values_list('pk', flat=True)

    delete_qs = model.objects.filter(pk__in=to_delete)
    delete_qs._raw_delete(delete_qs.db)


def delete_duplicates(apps, schema_editor):
    MaterialStorage = apps.get_model('herders', 'MaterialStorage')
    MonsterShrineStorage = apps.get_model('herders', 'MonsterShrineStorage')
    MonsterPiece = apps.get_model('herders', 'MonsterPiece')

    do_delete(MaterialStorage, 'item')
    do_delete(MonsterShrineStorage, 'item')
    do_delete(MonsterPiece, 'monster')

def noop(*args, **kwargs):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('bestiary', '0028_auto_20201111_1135'),
        ('herders', '0022_auto_20201229_1609'),
    ]

    operations = [
        migrations.RunPython(delete_duplicates, noop),
    ]

@@ -515,59 +515,271 @@
     
-delete_qs = model.objects.filter(pk__in=to_delete)%0A
+to_delete_len = to_delete.count()%0A    chunk_size = 1000%0A    iter_chunk = 0%0A%0A    while iter_chunk * chunk_size %3C to_delete_len:%0A        delete_qs = model.objects.filter(pk__in=to_delete%5Biter_chunk*chunk_size:min((iter_chunk + 1) * chunk_size, to_delete_len)%5D)%0A    
     
@@ -814,16 +814,40 @@
 _qs.db)%0A
+        iter_chunk += 1%0A
 %0A%0Adef de

Update 0023_auto_20210117_1748.py