From 044ecd61f857d98074b519ae209c90da648942a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 12 Mar 2009 15:52:44 +0100 Subject: [PATCH] Fix the history table creation - create missing field added by others modules for issue857 - don't update history record with default value --- trytond/backend/postgresql/table.py | 21 +++++++++++---------- trytond/model/modelsql.py | 7 +++++-- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/trytond/backend/postgresql/table.py b/trytond/backend/postgresql/table.py index d995550d3b..6a8c74bb30 100644 --- a/trytond/backend/postgresql/table.py +++ b/trytond/backend/postgresql/table.py @@ -183,16 +183,17 @@ class TableHandler(TableHandlerInterface): self.cursor.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self.table_name, column_name, column_type)) - # check if table is non-empty: - self.cursor.execute('SELECT 1 FROM "%s" limit 1' % self.table_name) - if self.cursor.rowcount: - # Populate column with default values: - default = None - if default_fun is not None: - default = default_fun(self.cursor, 0, {}) - self.cursor.execute('UPDATE "' + self.table_name + '" '\ - 'SET "' + column_name + '" = %s', - (column_format(default),)) + if column_format: + # check if table is non-empty: + self.cursor.execute('SELECT 1 FROM "%s" limit 1' % self.table_name) + if self.cursor.rowcount: + # Populate column with default values: + default = None + if default_fun is not None: + default = default_fun(self.cursor, 0, {}) + self.cursor.execute('UPDATE "' + self.table_name + '" '\ + 'SET "' + column_name + '" = %s', + (column_format(default),)) self._update_definitions() diff --git a/trytond/model/modelsql.py b/trytond/model/modelsql.py index 0681a5bbc3..e5848e74ca 100644 --- a/trytond/model/modelsql.py +++ b/trytond/model/modelsql.py @@ -133,8 +133,7 @@ class ModelSQL(ModelStorage): hasattr(field, 'size') and field.size or None) if self._history: history_table.add_raw_column(field_name, - FIELDS[field._type].sql_type(field), - FIELDS[field._type].sql_format) + FIELDS[field._type].sql_type(field), None) if isinstance(field, (fields.Integer, fields.Float)): table.db_default(field_name, 0) @@ -170,6 +169,10 @@ class ModelSQL(ModelStorage): table.add_constraint(ident, constraint) if self._history: + for column_name in table._columns: + if not history_table.column_exist(column_name): + history_table.add_raw_column(column_name, + table._columns[column_name], None) cursor.execute('SELECT id FROM "' + self._table + '"') if cursor.rowcount: cursor.execute('SELECT id FROM "' + self._table + '__history"')