Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Added unique check constraint in M2M models
NyanKiyoshi09210bac1110
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex dffbbf9056..3d1784452b 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -82,6 +82,7 @@ All notable, unreleased changes to this project will be documented in this file.- CustomerPasswordReset+- Users cannot add multiple times the same product into a collection anymore - #4518 by @NyanKiyoshi## 2.8.0diff --git a/saleor/product/migrations/0103_remove_dups.py b/saleor/product/migrations/0103_remove_dups.pynew file mode 100644index 0000000000..505c81bb57--- /dev/null+++ b/saleor/product/migrations/0103_remove_dups.py@@ -0,0 +1,58 @@+# Generated by Django 2.2.3 on 2019-07-24 07:56++from django.db import migrations+from django.db.models import Count+++def remove_duplicates(apps, schema_editor):+ """Removes any duplicated M2M, and keep only one of them.++ First we select the duplicates, by grouping them and counting them:++ SELECT+ collection_id, product_id, COUNT(*)+ FROM+ public.product_collectionproduct+ GROUP BY+ collection_id, product_id+ HAVING+ COUNT(*) > 1++ Then we retrieve all of them except one (LIMIT = `duplicate_count - 1`).++ Once we have them, we delete each of them manually (cannot directly delete by using+ LIMIT).+ """++ CollectionProduct = apps.get_model("product", "CollectionProduct")++ duplicates = (+ CollectionProduct.objects.values("collection_id", "product_id")+ .annotate(duplicate_count=Count("*"))+ .filter(duplicate_count__gt=1)+ )++ for duplicate in duplicates:+ dup_count = duplicate.pop("duplicate_count")+ delete_limit = dup_count - 1+ entries_to_delete = CollectionProduct.objects.filter(**duplicate)[:delete_limit]+ for entry in entries_to_delete:+ entry.delete()+++class Migration(migrations.Migration):++ dependencies = [("product", "0102_attribute_available_in_grid")]++ operations = [+ migrations.RunPython(remove_duplicates),+ migrations.AlterUniqueTogether(+ name="attributeproduct", unique_together={("attribute", "product_type")}+ ),+ migrations.AlterUniqueTogether(+ name="attributevariant", unique_together={("attribute", "product_type")}+ ),+ migrations.AlterUniqueTogether(+ name="collectionproduct", unique_together={("collection", "product")}+ ),+ ]diff --git a/saleor/product/models.py b/saleor/product/models.pyindex 0bdf347275..188f2d58b0 100644--- a/saleor/product/models.py+++ b/saleor/product/models.py@@ -461,6 +461,9 @@ class AttributeProduct(SortableModel):ProductType, related_name="attributeproduct", on_delete=models.CASCADE)+ class Meta:+ unique_together = (("attribute", "product_type"),)+def get_ordering_queryset(self):return self.product_type.attributeproduct.all()@@ -473,6 +476,9 @@ class AttributeVariant(SortableModel):ProductType, related_name="attributevariant", on_delete=models.CASCADE)+ class Meta:+ unique_together = (("attribute", "product_type"),)+def get_ordering_queryset(self):return self.product_type.attributevariant.all()@@ -666,6 +672,9 @@ class CollectionProduct(SortableModel):Product, related_name="collectionproduct", on_delete=models.CASCADE)+ class Meta:+ unique_together = (("collection", "product"),)+def get_ordering_queryset(self):return self.product.collectionproduct.all()