From f4fb494f322c378bac5b606deeb168e00e7246fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 13 Jan 2026 16:50:08 +0100 Subject: [PATCH] Ensure to always surround USING expression with parenthesis in CREATE INDEX Closes #14487 (grafted from b45a29b26853324f65adc8c49b59a62d6f591529) --HG-- branch : 7.0 --- trytond/trytond/backend/postgresql/table.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/trytond/trytond/backend/postgresql/table.py b/trytond/trytond/backend/postgresql/table.py index b11dad6bec..c51d437bfa 100644 --- a/trytond/trytond/backend/postgresql/table.py +++ b/trytond/trytond/backend/postgresql/table.py @@ -4,6 +4,7 @@ import logging import re from psycopg2.sql import SQL, Identifier +from sql import Column from trytond.backend.table import ( IndexTranslatorInterface, TableHandlerInterface) @@ -581,11 +582,18 @@ class IndexMixin: @classmethod def _get_expression_variables(cls, expression, usage): variables = { - 'expression': SQL(str(expression)), 'collate': SQL(''), 'opclass': SQL(''), 'order': SQL(''), } + if isinstance(expression, Column): + variables['expression'] = SQL(str(expression)) + else: + expression_str = str(expression) + if expression_str.startswith('(') and expression_str.endswith(')'): + variables['expression'] = SQL(expression) + else: + variables['expression'] = SQL(f'({expression})') if usage.options.get('collation'): variables['collate'] = SQL('COLLATE {}').format( usage.options['collation'])