# Buggy code
def get_or_create_warehouse(apps):
    Warehouse = apps.get_model("warehouse", "Warehouse")
    ShippingZone = apps.get_model("shipping", "ShippingZone")
    Site = apps.get_model("sites", "Site")

    warehouses = Warehouse.objects.annotate(
        zones_count=models.Count("shipping_zones")
    ).filter(zones_count=ShippingZone.objects.count())
    if warehouses.first() is not None:
        return warehouses.first()

    site_settings = Site.objects.get_current().settings
    address = getattr(site_settings, "company_address", None)
    if address is None:
        Address = apps.get_model("account", "Address")
        address = Address.objects.create()

    warehouse = Warehouse.objects.create(address=address)
    warehouse.shipping_zones.add(*ShippingZone.objects.all())
    return warehouse


# 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 get_or_create_warehouse(apps):
    Warehouse = apps.get_model("warehouse", "Warehouse")
    ShippingZone = apps.get_model("shipping", "ShippingZone")
    Site = apps.get_model("sites", "Site")

    warehouses = Warehouse.objects.annotate(
        zones_count=models.Count("shipping_zones")
    ).filter(zones_count=ShippingZone.objects.count())
    if warehouses.first() is not None:
        return warehouses.first()

    site_settings = Site.objects.get_current().settings
    address = getattr(site_settings, "company_address", None)
    if address is None:
        Address = apps.get_model("account", "Address")
        address = Address.objects.create()

    warehouse = Warehouse.objects.create(name="Default warehouse", address=address)
    warehouse.shipping_zones.add(*ShippingZone.objects.all())
    return warehouse