Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add db_index=False to field in product

Ania Warzecha642339e19b6d
diff --git a/saleor/attribute/migrations/0035_assignedproductattributevalue_product_add_index.py b/saleor/attribute/migrations/0035_assignedproductattributevalue_product_add_index.py
index 29fe8e6485..aad51e4804 100644
--- a/saleor/attribute/migrations/0035_assignedproductattributevalue_product_add_index.py
+++ b/saleor/attribute/migrations/0035_assignedproductattributevalue_product_add_index.py
@@ -1,6 +1,8 @@
# Generated by Django 3.2.20 on 2023-08-24 11:25
from django.db import migrations
+from django.contrib.postgres.operations import AddIndexConcurrently
+from django.contrib.postgres.indexes import BTreeIndex
class Migration(migrations.Migration):
@@ -11,12 +13,19 @@ class Migration(migrations.Migration):
]
operations = [
+ # We have introduced a non-concurrent index creation in one of previous
+ # migrations. This has only been applied to dev environments of people
+ # who work on the main branch.
migrations.RunSQL(
"""
- CREATE INDEX CONCURRENTLY IF NOT EXISTS
- attribute_assignedproductattributevalue_product_id_805656f1
- ON attribute_assignedproductattributevalue USING btree
- (product_id ASC NULLS LAST);
+ DROP INDEX attribute_assignedproductattributevalue_product_id_805656f1
+ ON attribute_assignedproductattributevalue;
"""
- )
+ ),
+ AddIndexConcurrently(
+ model_name="assignedproductattributevalue",
+ index=BTreeIndex(
+ fields=["product"], name="assignedprodattrval_product_idx"
+ ),
+ ),
]
diff --git a/saleor/attribute/models/product.py b/saleor/attribute/models/product.py
index bad26b3a08..6b38f805e4 100644
--- a/saleor/attribute/models/product.py
+++ b/saleor/attribute/models/product.py
@@ -1,3 +1,4 @@
+from django.contrib.postgres.indexes import BTreeIndex
from django.db import models
from ...core.models import SortableModel
@@ -23,11 +24,15 @@ class AssignedProductAttributeValue(SortableModel):
on_delete=models.CASCADE,
null=True,
blank=True,
+ db_index=False,
)
class Meta:
unique_together = (("value", "assignment"),)
ordering = ("sort_order", "pk")
+ indexes = [
+ BTreeIndex(fields=["product"], name="assignedprodattrval_product_idx")
+ ]
def get_ordering_queryset(self):
return self.assignment.productvalueassignment.all()