From 9db2aa49585dba78374c80e4f2d1c51f2b00be31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sun, 22 Mar 2009 23:24:12 +0100 Subject: [PATCH] Set default value for id and __id column for issue886 --- trytond/backend/postgresql/table.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/trytond/backend/postgresql/table.py b/trytond/backend/postgresql/table.py index eab527b759..f7602a63ed 100644 --- a/trytond/backend/postgresql/table.py +++ b/trytond/backend/postgresql/table.py @@ -23,22 +23,26 @@ class TableHandler(TableHandlerInterface): # Create new table if necessary if not self.table_exist(self.cursor, self.table_name): - self.cursor.execute('CREATE TABLE "%s" ' \ - '(id INTEGER NOT NULL)' % self.table_name) - if not self.history: - self.cursor.execute('ALTER TABLE "%s" ' \ - 'ADD PRIMARY KEY(id)' % self.table_name) + self.cursor.execute('CREATE TABLE "%s" ()' % self.table_name) + self._update_definitions() if 'id' not in self._columns: - self.cursor.execute('ALTER TABLE "%s" ' \ - 'ADD COLUMN id INTEGER NOT NULL' % self.table_name) if not self.history: + self.cursor.execute('ALTER TABLE "%s" ' \ + 'ADD COLUMN id INTEGER ' \ + 'DEFAULT nextval(\'%s\') NOT NULL' % \ + (self.table_name, self.sequence_name)) self.cursor.execute('ALTER TABLE "%s" ' \ 'ADD PRIMARY KEY(id)' % self.table_name) + else: + self.cursor.execute('ALTER TABLE "%s" ' \ + 'ADD COLUMN id INTEGER' % self.table_name) self._update_definitions() if self.history and not '__id' in self._columns: self.cursor.execute('ALTER TABLE "%s" ' \ - 'ADD COLUMN __id INTEGER NOT NULL' % self.table_name) + 'ADD COLUMN __id INTEGER ' \ + 'DEFAULT nextval(\'%s\') NOT NULL' % \ + (self.table_name, self.sequence_name)) self.cursor.execute('ALTER TABLE "%s" ' \ 'ADD PRIMARY KEY(__id)' % self.table_name) if self.history: @@ -49,6 +53,7 @@ class TableHandler(TableHandlerInterface): self.cursor.execute('ALTER TABLE "%s" ' \ 'ALTER id SET DEFAULT nextval(\'%s\')' % (self.table_name, self.sequence_name)) + self._update_definitions() @staticmethod def table_exist(cursor, table_name):