Skip to content

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

Fix how the product_id is assigned

Ania Warzecha4a6937d36d0f
diff --git a/saleor/attribute/tests/test_utils.py b/saleor/attribute/tests/test_utils.py
index 99e79f2cae..62d05228ba 100644
--- a/saleor/attribute/tests/test_utils.py
+++ b/saleor/attribute/tests/test_utils.py
@@ -121,7 +121,7 @@ def test_associate_attribute_to_variant_instance_multiple_values(
def test_associate_attribute_to_product_copies_data_over_to_new_field(
- product, attribute_value_generator
+ product, color_attribute
):
"""Ensure data is double writed.
@@ -129,25 +129,15 @@ def test_associate_attribute_to_product_copies_data_over_to_new_field(
value of AssignedProductAttribute.product is copied over to
AssignedProductAttributeValue.product.
"""
- old_assignment = product.attributes.first()
- assert old_assignment is not None, "The product doesn't have attribute-values"
- assert old_assignment.values.count() == 1
-
- attribute = old_assignment.attribute
- attribute_value_generator(
- attribute=attribute,
- slug="attr-value2",
- )
- values = attribute.values.all()
+ values = color_attribute.values.all()
# Assign new values
new_assignment = associate_attribute_values_to_instance(
- product, attribute, values[1], values[0]
+ product, color_attribute, values[0], values[1]
)
# Ensure the new assignment was created
- assert new_assignment.pk == old_assignment.pk
assert new_assignment.values.count() == 2
assert list(
- new_assignment.variantvalueassignment.values_list("value_id", "product_id")
+ new_assignment.productvalueassignment.values_list("value_id", "product_id")
) == [(values[0].pk, product.id), (values[1].pk, product.id)]
diff --git a/saleor/attribute/utils.py b/saleor/attribute/utils.py
index 08376c6164..a76c0360e1 100644
--- a/saleor/attribute/utils.py
+++ b/saleor/attribute/utils.py
@@ -42,6 +42,13 @@ def associate_attribute_values_to_instance(
# Associate the attribute and the passed values
assignment = _associate_attribute_to_instance(instance, attribute.pk)
assignment.values.set(values)
+
+ # While migrating to a new structure we need to make sure we also
+ # copy the assigned product to AssignedProductAttributeValue
+ # where it will live after issue #12881 will be implemented
+ AssignedProductAttributeValue.objects.filter(assignment_id=assignment.pk).update(
+ product_id=instance.pk
+ )
sort_assigned_attribute_values(instance, assignment, values)
return assignment
@@ -71,10 +78,6 @@ def _associate_attribute_to_instance(
assignment, _ = AssignedProductAttribute.objects.get_or_create(
product=instance, assignment=attribute_rel
)
- # While migrating to a new structure we need to make sure we also
- # copy the assigned product to AssignedProductAttributeValue
- # where it will live after issue #12881 will be implemented
- assignment.productvalueassignment.update(product_id=instance.pk)
elif isinstance(instance, ProductVariant):
attribute_rel = instance.product.product_type.attributevariant.get(
attribute_id=attribute_pk