74 lines
3.3 KiB
Diff
74 lines
3.3 KiB
Diff
diff -r 395b4d928bd4 trytond/trytond/model/modelsql.py
|
|
--- a/trytond/trytond/model/modelsql.py Tue Jan 12 11:42:44 2016 +0100
|
|
+++ b/trytond/trytond/model/modelsql.py Tue Jan 12 11:44:27 2016 +0100
|
|
@@ -5,7 +5,7 @@
|
|
from functools import reduce
|
|
from itertools import islice, izip, chain, ifilter
|
|
|
|
-from sql import Table, Column, Literal, Desc, Asc, Expression, Flavor
|
|
+from sql import Table, Column, Literal, Desc, Asc, Expression, Flavor, Null
|
|
from sql.functions import Now, Extract
|
|
from sql.conditionals import Coalesce
|
|
from sql.operators import Or, And, Operator
|
|
@@ -63,6 +63,8 @@
|
|
|
|
@classmethod
|
|
def __register__(cls, module_name):
|
|
+ sql_table = cls.__table__()
|
|
+ cursor = Transaction().cursor
|
|
TableHandler = backend.get('TableHandler')
|
|
super(ModelSQL, cls).__register__(module_name)
|
|
|
|
@@ -72,10 +74,10 @@
|
|
pool = Pool()
|
|
|
|
# create/update table in the database
|
|
- table = TableHandler(Transaction().cursor, cls, module_name)
|
|
+ table = TableHandler(cursor, cls, module_name)
|
|
if cls._history:
|
|
- history_table = TableHandler(Transaction().cursor, cls,
|
|
- module_name, history=True)
|
|
+ history_table = TableHandler(
|
|
+ cursor, cls, module_name, history=True)
|
|
history_table.index_action('id', action='add')
|
|
|
|
for field_name, field in cls._fields.iteritems():
|
|
@@ -139,17 +141,24 @@
|
|
if isinstance(field, fields.Many2One) \
|
|
and field.model_name == cls.__name__ \
|
|
and field.left and field.right:
|
|
- cls._rebuild_tree(field_name, None, 0)
|
|
+ left_default = cls._defaults.get(field.left, lambda: None)()
|
|
+ right_default = cls._defaults.get(field.right, lambda: None)()
|
|
+ cursor.execute(*sql_table.select(sql_table.id,
|
|
+ where=(Column(sql_table, field.left) == left_default)
|
|
+ | (Column(sql_table, field.left) == Null)
|
|
+ | (Column(sql_table, field.right) == right_default)
|
|
+ | (Column(sql_table, field.right) == Null),
|
|
+ limit=1))
|
|
+ if cursor.fetchone():
|
|
+ cls._rebuild_tree(field_name, None, 0)
|
|
|
|
for ident, constraint, _ in cls._sql_constraints:
|
|
table.add_constraint(ident, constraint)
|
|
|
|
if cls._history:
|
|
cls._update_history_table()
|
|
- cursor = Transaction().cursor
|
|
- table = cls.__table__()
|
|
history_table = cls.__table_history__()
|
|
- cursor.execute(*table.select(table.id))
|
|
+ cursor.execute(*sql_table.select(sql_table.id))
|
|
if cursor.fetchone():
|
|
cursor.execute(*history_table.select(history_table.id))
|
|
if not cursor.fetchone():
|
|
@@ -157,7 +166,7 @@
|
|
if not hasattr(f, 'set')]
|
|
cursor.execute(*history_table.insert(
|
|
[Column(history_table, c) for c in columns],
|
|
- table.select(*(Column(table, c)
|
|
+ sql_table.select(*(Column(sql_table, c)
|
|
for c in columns))))
|
|
cursor.execute(*history_table.update(
|
|
[history_table.write_date], [None]))
|