# Buggy code
def update_non_unique_slugs_for_models(apps, schema_editor):
    models_to_update = ["Category", "Collection"]

    for model in models_to_update:
        Model = apps.get_model("product", model)

        duplicated_slugs = (
            Model.objects.all()
            .values("slug")
            .annotate(duplicated_slug_num=models.Count("slug"))
            .filter(duplicated_slug_num__gt=1)
        )

        slugs_counter = defaultdict(int)
        for data in duplicated_slugs:
            slugs_counter[data["slug"]] = data["duplicated_slug_num"]

        queryset = Model.objects.filter(slug__in=slugs_counter.keys()).order_by("name")

        for instance in queryset:
            slugs_counter[instance.slug] -= 1
            slug = update_slug_to_unique_value(instance.slug, slugs_counter)
            instance.slug = slug
            slugs_counter[slug] += 1


# Traceback (django.db.utils.IntegrityError)
Applying product.0111_auto_20191209_0437... OK
Applying product.0112_auto_20200129_0050...Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.NotNullViolation: column "slug" contains null values


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 231, in handle
post_migrate_state = executor.migrate(
File "/usr/local/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 "/usr/local/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 "/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/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 "/usr/local/lib/python3.8/site-packages/django/db/migrations/operations/fields.py", line 249, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 564, in alter_field
self._alter_field(model, old_field, new_field, old_type, new_type,
File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/schema.py", line 152, in _alter_field
super()._alter_field(
File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 710, in _alter_field
self.execute(
File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 142, in execute
cursor.execute(sql, params)
File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: column "slug" contains null values

# Fixed code
def update_non_unique_slugs_for_models(apps, schema_editor):
    models_to_update = ["Category", "Collection"]

    for model in models_to_update:
        Model = apps.get_model("product", model)

        duplicated_slugs = (
            Model.objects.all()
            .values("slug")
            .annotate(duplicated_slug_num=models.Count("slug"))
            .filter(duplicated_slug_num__gt=1)
        )

        slugs_counter = defaultdict(int)
        for data in duplicated_slugs:
            slugs_counter[data["slug"]] = data["duplicated_slug_num"]

        queryset = Model.objects.filter(slug__in=slugs_counter.keys()).order_by("name")

        for instance in queryset:
            slugs_counter[instance.slug] -= 1
            slug = update_slug_to_unique_value(instance.slug, slugs_counter)
            instance.slug = slug
            instance.save(update_fields=["slug"])
            slugs_counter[slug] += 1