details:   https://code.tryton.org/tryton/commit/a268eb842452
branch:    default
user:      Cédric Krier <[email protected]>
date:      Sat Dec 13 14:33:42 2025 +0100
description:
        Use the right primary column name for PostgreSQL database method

        On history table the primary column is `__id` instead of `id`.
diffstat:

 trytond/trytond/backend/postgresql/database.py |  11 ++++++++---
 1 files changed, 8 insertions(+), 3 deletions(-)

diffs (39 lines):

diff -r e277a4c8d43b -r a268eb842452 
trytond/trytond/backend/postgresql/database.py
--- a/trytond/trytond/backend/postgresql/database.py    Sat Dec 13 14:31:19 
2025 +0100
+++ b/trytond/trytond/backend/postgresql/database.py    Sat Dec 13 14:33:42 
2025 +0100
@@ -451,27 +451,32 @@
         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]

Reply via email to