diff --git a/trytond/trytond/backend/postgresql/database.py b/trytond/trytond/backend/postgresql/database.py index 05bb114964..87575dc2ee 100644 --- a/trytond/trytond/backend/postgresql/database.py +++ b/trytond/trytond/backend/postgresql/database.py @@ -420,12 +420,15 @@ class Database(DatabaseInterface): def nextid(self, connection, table): cursor = connection.cursor() - cursor.execute("SELECT NEXTVAL(%s)", (table + '_id_seq',)) + cursor.execute( + "SELECT nextval(format(%s, %s))", ('%I', table + '_id_seq',)) return cursor.fetchone()[0] def setnextid(self, connection, table, value): cursor = connection.cursor() - cursor.execute("SELECT SETVAL(%s, %s)", (table + '_id_seq', value)) + cursor.execute( + "SELECT setval(format(%s, %s), %s)", + ('%I', table + '_id_seq', value)) def currid(self, connection, table): cursor = connection.cursor() diff --git a/trytond/trytond/backend/postgresql/table.py b/trytond/trytond/backend/postgresql/table.py index e983479f12..927c8f6407 100644 --- a/trytond/trytond/backend/postgresql/table.py +++ b/trytond/trytond/backend/postgresql/table.py @@ -56,9 +56,9 @@ class TableHandler(TableHandlerInterface): cursor.execute( SQL( "ALTER TABLE {} ADD COLUMN id INTEGER " - "DEFAULT nextval(%s) NOT NULL").format( + "DEFAULT nextval(format(%s, %s)) NOT NULL").format( Identifier(self.table_name)), - (self.sequence_name,)) + ('%I', self.sequence_name)) cursor.execute( SQL('ALTER TABLE {} ADD PRIMARY KEY(id)') .format(Identifier(self.table_name))) @@ -71,27 +71,31 @@ class TableHandler(TableHandlerInterface): cursor.execute( SQL( "ALTER TABLE {} ADD COLUMN __id INTEGER " - "DEFAULT nextval(%s) NOT NULL").format( + "DEFAULT nextval(format(%s, %s)) NOT NULL").format( Identifier(self.table_name)), - (self.sequence_name,)) + ('%I', self.sequence_name)) cursor.execute( SQL('ALTER TABLE {} ADD PRIMARY KEY(__id)') .format(Identifier(self.table_name))) else: - default = "nextval('%s'::regclass)" % self.sequence_name + default = ( + "nextval((format('%%I'::text, '%s'))::regclass)" + % self.sequence_name) if self.history: if self._columns['__id']['default'] != default: cursor.execute( SQL("ALTER TABLE {} " - "ALTER __id SET DEFAULT nextval(%s::regclass)") + "ALTER __id SET " + "DEFAULT nextval(format(%s, %s))") .format(Identifier(self.table_name)), - (self.sequence_name,)) + ('%I', self.sequence_name)) if self._columns['id']['default'] != default: cursor.execute( SQL("ALTER TABLE {} " - "ALTER id SET DEFAULT nextval(%s::regclass)") + "ALTER id SET " + "DEFAULT nextval(format(%s, %s))") .format(Identifier(self.table_name)), - (self.sequence_name,)) + ('%I', self.sequence_name,)) self._update_definitions() @staticmethod