# Buggy code
def create_unique_slug_for_warehouses(apps, schema_editor):
    Warehouse = apps.get_model("warehouse", "Warehouse")

    warehouses = (
        Warehouse.objects.filter(slug__isnull=True).order_by(Lower("name")).iterator()
    )
    previous_char = ""
    slug_values = []
    for warehouse in warehouses:
        first_char = warehouse.name[0].lower()
        if first_char != previous_char:
            previous_char = first_char
            slug_values = list(
                Warehouse.objects.filter(slug__istartswith=first_char).values_list(
                    "slug", flat=True
                )
            )

        slug = generate_unique_slug(warehouse, slug_values)
        warehouse.slug = slug
        warehouse.save(update_fields=["slug"])
        slug_values.append(slug)


# 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_unique_slug_for_warehouses(apps, schema_editor):
    Warehouse = apps.get_model("warehouse", "Warehouse")

    warehouses = (
        Warehouse.objects.filter(slug__isnull=True).order_by(Lower("name")).iterator()
    )
    previous_char = None
    slug_values = []
    for warehouse in warehouses:
        if warehouse.name:
            first_char = warehouse.name[0].lower()
            if first_char != previous_char:
                previous_char = first_char
                slug_values = list(
                    Warehouse.objects.filter(slug__istartswith=first_char).values_list(
                        "slug", flat=True
                    )
                )
        elif previous_char is None:
            previous_char = ""
            slug_values = list(
                Warehouse.objects.filter(
                    slug__istartswith=DEFAULT_SLUG_VALUE
                ).values_list("slug", flat=True)
            )

        slug = generate_unique_slug(warehouse, slug_values)
        warehouse.slug = slug
        warehouse.save(update_fields=["slug"])
        slug_values.append(slug)