Always rebuild MPTT tree in case of nested creation

because _update_tree can not find children with left/right.

issue5095
review24571002
This commit is contained in:
Cédric Krier
2015-11-02 11:41:04 +01:00
parent edf80492d9
commit 5c062ebeea
+16 -2
View File
@@ -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)