From cf8702f18558b366d2d372e782f19b7f424574a7 Mon Sep 17 00:00:00 2001 From: Bertrand Chenal Date: Tue, 29 Jul 2008 17:57:52 +0200 Subject: [PATCH] Add default value in the db for integer and boolean field. --- trytond/osv/orm.py | 12 ++++++++---- trytond/sql_db.py | 12 ++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/trytond/osv/orm.py b/trytond/osv/orm.py index 02ee76879c..cadfb04dc9 100644 --- a/trytond/osv/orm.py +++ b/trytond/osv/orm.py @@ -425,15 +425,13 @@ class ORM(object): continue if field._classic_write: - if isinstance(field, (fields.Integer, fields.Float)): - default = 0 #XXX why ? - if field_name in self._defaults: default_fun = self._defaults[field_name] def unpack_wrapper(fun): def unpack_result(*a): - try: # XXX ugly hack + try: # XXX ugly hack: some default fct try + # to access the non-existing table result = fun(*a) except: return None @@ -448,6 +446,12 @@ class ORM(object): table.add_raw_column( field_name, field.sql_type(), default_fun, field.size) + if isinstance(field, (fields.Integer, fields.Float)): + table.db_default(field_name, 0) + + if isinstance(field, fields.Boolean): + table.db_default(field_name, False) + if isinstance(field, fields.Many2One): if field._obj in ('res.user', 'res.group'): ref = field._obj.replace('.','_') diff --git a/trytond/sql_db.py b/trytond/sql_db.py index fce12fbce2..ca3c0e9634 100644 --- a/trytond/sql_db.py +++ b/trytond/sql_db.py @@ -328,6 +328,10 @@ class table_handler: "DROP COLUMN temp_change_size" % \ (self.table_name,)) + def db_default(self, column_name, value): + self.cursor.execute('ALTER TABLE "' + self.table_name + '" ' \ + 'ALTER COLUMN "' + column_name + '" SET DEFAULT %s', + (value,)) def add_raw_column(self, column_name, column_type, default_fun=None, field_size=None, migrate = True): @@ -365,13 +369,13 @@ class table_handler: (self.table_name, column_name, column_type)) if default_fun is not None: default = default_fun(self.cursor, 0, {}) - if default: # XXX What about a boolean who's default false - # or an default integer of 0 .. all default - # fun should return None instead of false when - # there is no default to put. (see action.py l.230) + if (default is not False) and (default is not False): self.cursor.execute("UPDATE " + self.table_name + " "\ "SET \"" + column_name + "\" = %s", (default,)) + else: + self.cursor.execute("UPDATE " + self.table_name + " "\ + "SET \"" + column_name + "\" = NULL") def add_m2m(self, column_name, other_table, relation_table, rtable_from, rtable_to): if not table_exist(self.cursor, other_table):