1. Gustavo Picón
  2. django-treebeard
  3. Issues
Issue #49 resolved

ns_tree multi-db save error

Natalia Stepina
created an issue

If model not in the default database, exception occures on save. Patch is attached.

Tested on openSuse 11.3, python 2.6.5, Django 1.2.1, django-treebeard changeset 947c99b3b749.

Comments (2)

  1. Natalia Stepina reporter

    In case multidb.patch could not be opened, here it is:

    diff --git a/treebeard/ns_tree.py b/treebeard/ns_tree.py
    old mode 100644
    new mode 100755
    index f1a2a73..0ab86dc
    --- a/treebeard/ns_tree.py
    +++ b/treebeard/ns_tree.py
    @@ -6,10 +6,24 @@ from django.db.models import Q
     from django.core import serializers
     from django.db import models, transaction, connection
     
    +try:
    +    from django.db import connections, router
    +except ImportError:
    +    # multi db support was new in django 1.2
    +    connections = None
    +    router = None
    +
     from treebeard.models import Node
     from treebeard.exceptions import InvalidMoveToDescendant
     
     
    +def _get_connection(cls):
    +    if connections is None:
    +        return connection
    +    else:
    +        return connections[router.db_for_write(cls)]
    +
    +
     class NS_NodeQuerySet(models.query.QuerySet):
         """
         Custom queryset for the tree node manager.
    @@ -29,7 +43,7 @@ class NS_NodeQuerySet(models.query.QuerySet):
                 # delete method and let it handle the removal of the user's
                 # foreign keys...
                 super(NS_NodeQuerySet, self).delete()
    -            cursor = connection.cursor()
    +            cursor = _get_connection(self.model).cursor()
     
                 # Now closing the gap (Celko's trees book, page 62)
                 # We do this for every gap that was left in the tree when the nodes
    @@ -136,7 +150,7 @@ class NS_Node(Node):
                   '                ELSE rgt END ' \
                   ' WHERE rgt >= %(parent_rgt)d AND ' \
                   '       tree_id = %(tree_id)s' % {
    -                  'table': connection.ops.quote_name(cls._meta.db_table),
    +                  'table': _get_connection(cls).ops.quote_name(cls._meta.db_table),
                       'parent_rgt': rgt,
                       'tree_id': tree_id,
                       'lftop': lftop,
    @@ -148,7 +162,7 @@ class NS_Node(Node):
             sql = 'UPDATE %(table)s ' \
                   ' SET tree_id = tree_id+1 ' \
                   ' WHERE tree_id >= %(tree_id)d' % {
    -                  'table': connection.ops.quote_name(cls._meta.db_table),
    +                  'table': _get_connection(cls).ops.quote_name(cls._meta.db_table),
                       'tree_id': tree_id}
             return sql, []
     
    @@ -180,7 +194,7 @@ class NS_Node(Node):
     
             newobj._cached_parent_obj = self
     
    -        cursor = connection.cursor()
    +        cursor = _get_connection(self.__class__).cursor()
             cursor.execute(sql, params)
     
             # saving the instance before returning it
    @@ -274,7 +288,7 @@ class NS_Node(Node):
     
             # saving the instance before returning it
             if sql:
    -            cursor = connection.cursor()
    +            cursor = _get_connection(self.__class__).cursor()
                 cursor.execute(sql, params)
             newobj.save()
     
    @@ -346,7 +360,7 @@ class NS_Node(Node):
                     target = siblings[0]
     
             # ok let's move this
    -        cursor = connection.cursor()
    +        cursor = _get_connection(self.__class__).cursor()
             move_right = cls._move_right
             gap = self.rgt - self.lft + 1
             sql = None
    @@ -396,7 +410,7 @@ class NS_Node(Node):
                   "     depth = depth + %(depthdiff)d " \
                   " WHERE tree_id = %(from_tree)d AND " \
                   "     lft BETWEEN %(fromlft)d AND %(fromrgt)d" % {
    -                  'table': connection.ops.quote_name(cls._meta.db_table),
    +                  'table': _get_connection(cls).ops.quote_name(cls._meta.db_table),
                       'from_tree': fromobj.tree_id,
                       'target_tree': target_tree,
                       'jump': newpos - fromobj.lft,
    @@ -426,7 +440,7 @@ class NS_Node(Node):
                   ' WHERE (lft > %(drop_lft)d ' \
                   '     OR rgt > %(drop_lft)d) AND '\
                   '     tree_id=%(tree_id)d' % {
    -                  'table': connection.ops.quote_name(cls._meta.db_table),
    +                  'table': _get_connection(cls).ops.quote_name(cls._meta.db_table),
                       'gapsize': drop_rgt - drop_lft + 1,
                       'drop_lft': drop_lft,
                       'tree_id': tree_id}
    
  2. Log in to comment