# Buggy code
def create_allocations(apps, schema_editor):
    Allocation = apps.get_model("warehouse", "Allocation")
    OrderLine = apps.get_model("order", "OrderLine")
    Warehouse = apps.get_model("warehouse", "Warehouse")
    for warehouse in Warehouse.objects.iterator():
        shipping_zone_pk = warehouse.shipping_zones.first().pk
        for order_line in OrderLine.objects.filter(
            order__shipping_method__shipping_zone__pk=shipping_zone_pk,
        ).iterator():
            quantity_unfulfilled = order_line.quantity - order_line.quantity_fulfilled
            if quantity_unfulfilled > 0 and order_line.variant:
                create_allocation(
                    order_line.variant,
                    warehouse,
                    order_line,
                    quantity_unfulfilled,
                    Allocation,
                )


# Traceback (IndexError)
Applying warehouse.0003_warehouse_slug...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/timur/Code/pyenv/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/Users/timur/Code/pyenv/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/timur/Code/pyenv/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/timur/Code/pyenv/lib/python3.8/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/Users/timur/Code/pyenv/lib/python3.8/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/Users/timur/Code/pyenv/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 231, in handle
post_migrate_state = executor.migrate(
File "/Users/timur/Code/pyenv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/Users/timur/Code/pyenv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Users/timur/Code/pyenv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/timur/Code/pyenv/lib/python3.8/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Users/timur/Code/pyenv/lib/python3.8/site-packages/django/db/migrations/operations/special.py", line 190, in database_forwards
self.code(from_state.apps, schema_editor)
File "/Users/timur/Code/saleor/saleor/warehouse/migrations/0003_warehouse_slug.py", line 17, in create_unique_slug_for_warehouses
first_char = warehouse.name[0].lower()
IndexError: string index out of range

# Fixed code
def create_allocations(apps, schema_editor):
    Allocation = apps.get_model("warehouse", "Allocation")
    OrderLine = apps.get_model("order", "OrderLine")
    Warehouse = apps.get_model("warehouse", "Warehouse")
    for warehouse in Warehouse.objects.iterator():
        shipping_zone = warehouse.shipping_zones.first()
        if not shipping_zone:
            continue
        shipping_zone_pk = shipping_zone.pk
        for order_line in OrderLine.objects.filter(
            order__shipping_method__shipping_zone__pk=shipping_zone_pk,
        ).iterator():
            quantity_unfulfilled = order_line.quantity - order_line.quantity_fulfilled
            if quantity_unfulfilled > 0 and order_line.variant:
                create_allocation(
                    order_line.variant,
                    warehouse,
                    order_line,
                    quantity_unfulfilled,
                    Allocation,
                )