From 22f151fbdc3af3040f9e9020d6bd5200a70ea257 Mon Sep 17 00:00:00 2001 From: Julio Alberto Bravo Montanos Date: Sun, 16 Mar 2025 23:30:23 +0100 Subject: [PATCH] Make product attribute name unique The name of the attributes are the keys of the attributes Dict field of product so they must be uniquely identified at least per sets. But as an attribute can be reused on different sets, the unique constraint must be set on the name. Closes #13574 --- modules/product_attribute/message.xml | 3 +++ modules/product_attribute/product.py | 11 ++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/modules/product_attribute/message.xml b/modules/product_attribute/message.xml index 69faa09f32..f3534aadd5 100644 --- a/modules/product_attribute/message.xml +++ b/modules/product_attribute/message.xml @@ -6,5 +6,8 @@ this repository contains the full copyright notices and license terms. --> %(label)s: %(value)s + + The name of the product attribute must be unique. + diff --git a/modules/product_attribute/product.py b/modules/product_attribute/product.py index a6eacd5752..0a77e25927 100644 --- a/modules/product_attribute/product.py +++ b/modules/product_attribute/product.py @@ -1,7 +1,7 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. from trytond.i18n import gettext -from trytond.model import DictSchemaMixin, ModelSQL, ModelView, fields +from trytond.model import DictSchemaMixin, ModelSQL, ModelView, Unique, fields from trytond.pool import PoolMeta from trytond.pyson import Eval @@ -21,6 +21,15 @@ class Attribute(DictSchemaMixin, ModelSQL, ModelView): 'attribute', 'attribute_set', 'Sets', help="Add sets to the attribute.") + @classmethod + def __setup__(cls): + super().__setup__() + t = cls.__table__() + cls._sql_constraints += [ + ('name_unique', Unique(t, t.name), + 'product_attribute.msg_product_attribute_name_unique'), + ] + class AttributeAttributeSet(ModelSQL): __name__ = 'product.attribute-product.attribute-set'