2

我有以下自引用(树)节点,并希望通过计算的属性uuid_path和过滤/排序name_path

class Node (db.Model):
    id = db.Column (db.Integer, db.Sequence ('node_id_seq'), primary_key=True)

    ###########################################################################

    root_id = db.Column (db.Integer, db.ForeignKey (id, ondelete='CASCADE'),
        index=True)

    nodes = db.relationship ('Node',
        cascade='all, delete-orphan', lazy='dynamic',
        primaryjoin='Node.id==Node.root_id',
        backref=db.backref ('root', remote_side=id))

    ###########################################################################

    _uuid = db.Column (db.String (36), nullable=False, index=True, unique=True,
        name = 'uuid')
    _name = db.Column (db.Unicode (256), nullable=False, index=True,
        name = 'name')

    ###########################################################################

    @hybrid_property
    def uuid (self):
        return self._uuid

    @hybrid_property
    def name (self):
        return self._name
    @name.setter
    def name (self, value):
        self._name = value

    ###########################################################################

    def __init__ (self, name, root, mime=None, uuid=None):

        self.root = root
        self._uuid = uuid if uuid else str (uuid_random ())
        self._name = unicode (name) if name is not None else None

    def __repr__ (self):

        return u'<Node@%x: %s>' % (self.id if self.id else 0, self._name)

    ###########################################################################

    @hybrid_property
    def uuid_path (self):
        node, path = self, []
        while node:

            path.insert (0, node.uuid)
            node = node.root

        return os.path.sep.join (path)

    @hybrid_property
    def name_path (self):
        node, path = self, []
        while node:

            path.insert (0, node.name)
            node = node.root

        return os.path.sep.join (path)

    ###########################################################################

如果我得到一个Node实例subnode并执行 egsubnode.name_path然后我得到正确的 eg root/subnode。但是,如果我尝试使用Node.name_path(用于过滤/排序),那么 SQLAlchemy 会抱怨:

Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with Node.root has an attribute 'name'.

我很确定我必须介绍以下内容:

class Node (db.Model):

    @hybrid_property
    def name_path (self):
        node, path = self, []
        while node:

            path.insert (0, node.name)
            node = node.root

        return os.path.sep.join (path)

    @name_path.expression
    def name_path (cls):
        ## Recursive SQL expression??

但是我很难为@name_path.expression(或@uuid_path.expression)得到正确的定义;它应该以某种方式指示 SQL 将路径从根节点传递到相关节点。

我不明白为什么这是必需的,因为我告诉 SQLAlchemy 迭代计算路径值。谢谢你的帮助。

4

2 回答 2

0

好吧,在使用 PostgreSQL 和 SQLAlchemy 进行调整之后,我想我有一个解决方案:(1)首先,我将查询编写为 SQL 中的函数,(2)其次提供正确的 SQLAlchemy 胶水:

SQL 部分使用WITH RECURSIVECTE:

CREATE OR REPLACE FUNCTION name_path(node)
  RETURNS text AS
$BODY$

WITH RECURSIVE graph (id, root_id, id_path, name_path) AS (
    SELECT n.id, n.root_id, ARRAY[n.id], ARRAY[n.name]
    FROM node n
UNION
    SELECT n.id, n.root_id, id_path||ARRAY[n.id], name_path||ARRAY[n.name]
    FROM node n, graph g
    WHERE n.root_id = g.id)

SELECT array_to_string (g.name_path, '/','.*')
FROM graph g
WHERE (g.id_path[1] = $1.base_id OR g.root_id IS NULL)
AND (g.id = $1.id)

$BODY$
  LANGUAGE sql STABLE
  COST 100;
ALTER FUNCTION name_path(node)
  OWNER TO webed;

和 SQLAlchemy 方面看起来像这样:

class NamePathColumn (ColumnClause):
    pass

@compiles (NamePathColumn)
def compile_name_path_column (element, compiler, **kwargs):
    return 'node.name_path' ## something missing?

class Node (db.Model):

    def get_path (self, field):

        @cache.version (key=[self.uuid, 'path', field])
        def cached_path (self, field):

            if self.root:
                return self.root.get_path (field) + [getattr (self, field)]
            else:
                return [getattr (self, field)]

        if field == 'uuid':
            return cached_path (self, field)
        else:
            return cached_path.uncached (self, field)

    @hybrid_property
    def name_path (self):
        return os.path.sep.join (self.get_path (field='name'))

    @name_path.expression
    def name_path (cls):
        return NamePathColumn (cls)

Node.name_path如果我在纯 Python 端,我会避免访问数据库,但如果我愿意,它可能会更快。唯一我仍然不太确定的是compile_name_path_column我没有考虑任何element, compiler, **kwargs参数,这让我有点怀疑。

在用 SA 和 PG 修补了大约 1.5 天后,我刚刚做好了这个,所以很有可能还有改进的空间。我非常感谢这种方法的任何评论。谢谢。

于 2013-01-24T15:54:48.527 回答
0

为了完整起见,我从他的https://gist.github.com/4625858中加入了 zzzeek 的反馈:

from sqlalchemy.sql.expression import ColumnElement ## !!
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.ext.compiler import compiles
from sqlalchemy import inspect

class UuidPathColumn (ColumnElement):
    def __init__(self, entity):
        insp = inspect (entity)
        self.entity = insp.selectable

@compiles (UuidPathColumn)
def compile_uuid_path_column (element, compiler, **kwargs):
    return "%s.uuid_path" % compiler.process (element.entity, ashint=True)

class NamePathColumn (ColumnElement):
    def __init__(self, entity):
        insp = inspect (entity)
        self.entity = insp.selectable

@compiles (NamePathColumn)
def compile_name_path_column (element, compiler, **kwargs):
    return "%s.name_path" % compiler.process (element.entity, ashint=True)

重要的是使用ColumnElement(而不是ColumnClause)让它起作用;相关代码可以在node.pyname_pathuuid_path 找到。这些东西已经用 SQLAlchemy 0.8 和 PostgreSQL 9.2 实现了。

于 2013-01-24T19:02:30.183 回答