Add default value in the db for integer and boolean field.
This commit is contained in:
1 parent
ed22afabfc
commit
cf8702f185
2 files changed
+16
-8
No files matched your search
+8
-4
@@ -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('.','_')
|
||||
|
||||
+8
-4
@@ -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):
|
||||
|
||||
Reference in new issue
Block a user