From 5c062ebeea7dd82e6edf11d704d2b46f1ad0dfe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 2 Nov 2015 11:41:04 +0100 Subject: [PATCH] Always rebuild MPTT tree in case of nested creation because _update_tree can not find children with left/right. issue5095 review24571002 --- trytond/model/modelsql.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/trytond/model/modelsql.py b/trytond/model/modelsql.py index 87fe6c4be5..067289828c 100644 --- a/trytond/model/modelsql.py +++ b/trytond/model/modelsql.py @@ -1281,10 +1281,24 @@ class ModelSQL(ModelStorage): raise Exception('ValidateError', 'You can not update fields: "%s", "%s"' % (field.left, field.right)) + + # Nested creation require a rebuild + # because initial values are 0 + # and thus _update_tree can not find the children + table = cls.__table__() + parent = cls.__table__() + cursor.execute(*table.join(parent, + condition=Column(table, field_name) == parent.id + ).select(table.id, + where=(Column(parent, field.left) == 0) + & (Column(parent, field.right) == 0))) + nested_create = cursor.fetchone() + if count is None: - cursor.execute(*cls.__table__().select(Count(Literal(1)))) + cursor.execute(*table.select(Count(Literal(1)))) count, = cursor.fetchone() - if len(ids) < count / 4: + + if not nested_create and len(ids) < count / 4: for id_ in ids: cls._update_tree(id_, field_name, field.left, field.right)