From efb8a5904b4d6b0b49e6ae26b2ce6ac3988bf6de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 13 Dec 2025 14:33:42 +0100 Subject: [PATCH] Use the right primary column name for PostgreSQL database method On history table the primary column is `__id` instead of `id`. (grafted from a268eb84245222f554f16b0cdc0e0b065b647724) --HG-- branch : 7.0 --- trytond/trytond/backend/postgresql/database.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/trytond/trytond/backend/postgresql/database.py b/trytond/trytond/backend/postgresql/database.py index 84a260e263..10faa3a00b 100644 --- a/trytond/trytond/backend/postgresql/database.py +++ b/trytond/trytond/backend/postgresql/database.py @@ -416,27 +416,32 @@ class Database(DatabaseInterface): return True def nextid(self, connection, table, count=1): + column = 'id' if not table.endswith('__history') else '__id' cursor = connection.cursor() cursor.execute( "SELECT nextval(pg_get_serial_sequence(format(%s, %s), %s)) " "FROM generate_series(1, %s)", - ('%I', table, 'id', count)) + ('%I', table, column, count)) if count == 1: return cursor.fetchone()[0] else: return [id for id, in cursor] def setnextid(self, connection, table, value): + if self.currid(connection, table) >= value: + return + column = 'id' if not table.endswith('__history') else '__id' cursor = connection.cursor() cursor.execute( "SELECT setval(pg_get_serial_sequence(format(%s, %s), %s), %s)", - ('%I', table, 'id', value)) + ('%I', table, column, value)) def currid(self, connection, table): + column = 'id' if not table.endswith('__history') else '__id' cursor = connection.cursor() cursor.execute( "SELECT pg_get_serial_sequence(format(%s, %s), %s)", - ('%I', table, 'id')) + ('%I', table, column)) sequence_name, = cursor.fetchone() cursor.execute(f"SELECT last_value FROM {sequence_name}") return cursor.fetchone()[0]