Repository: saleor/saleor

Issue:
psycopg2.errors.NotNullViolation: column "slug" contains null values
### What I'm trying to achieve
Upgrade from version 2.9.0 to 2.10.0-rc.1. Running the migrate command successfully.

Result:
```
 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
``` 



### Steps to reproduce the problem
1. Running 2.9.0 version with some data included
2. Upgrade the docker container, try to run the migrate command

### What I expected to happen
Run the migrate command successfully. The migration still seems to be very buggy. We already had issue before as seen here: #5391 

### Screenshots
<!-- If applicable, add screenshots to help explain your problem. -->

**System information**
Operating system:



Discussion:

Resolving patch:
diff --git a/saleor/product/migrations/0112_auto_20200129_0050.py b/saleor/product/migrations/0112_auto_20200129_0050.py
--- a/saleor/product/migrations/0112_auto_20200129_0050.py
+++ b/saleor/product/migrations/0112_auto_20200129_0050.py
@@ -27,6 +27,7 @@ def create_unique_slugs_for_producttypes(apps, schema_editor):
 
         slug = generate_unique_slug(product_type, slug_values)
         product_type.slug = slug
+        product_type.save(update_fields=["slug"])
         slug_values.append(slug)
 
 
@@ -66,6 +67,7 @@ def update_non_unique_slugs_for_models(apps, schema_editor):
             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
 
 
diff --git a/saleor/product/migrations/0114_auto_20200129_0815.py b/saleor/product/migrations/0114_auto_20200129_0815.py
--- a/saleor/product/migrations/0114_auto_20200129_0815.py
+++ b/saleor/product/migrations/0114_auto_20200129_0815.py
@@ -17,12 +17,15 @@ def create_unique_slug_for_products(apps, schema_editor):
         first_char = product.name[0].lower()
         if first_char != previous_char:
             previous_char = first_char
-            slug_values = Product.objects.filter(
-                slug__istartswith=first_char
-            ).values_list("slug", flat=True)
+            slug_values = list(
+                Product.objects.filter(slug__istartswith=first_char).values_list(
+                    "slug", flat=True
+                )
+            )
 
         slug = generate_unique_slug(product, slug_values)
         product.slug = slug
+        product.save(update_fields=["slug"])
         slug_values.append(slug)
 
 
diff --git a/saleor/warehouse/migrations/0003_warehouse_slug.py b/saleor/warehouse/migrations/0003_warehouse_slug.py
--- a/saleor/warehouse/migrations/0003_warehouse_slug.py
+++ b/saleor/warehouse/migrations/0003_warehouse_slug.py
@@ -17,12 +17,15 @@ def create_unique_slug_for_warehouses(apps, schema_editor):
         first_char = warehouse.name[0].lower()
         if first_char != previous_char:
             previous_char = first_char
-            slug_values = Warehouse.objects.filter(
-                slug__istartswith=first_char
-            ).values_list("slug", flat=True)
+            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)
 
 


Test patch: