SQLAlchemy:树节点中的递归混合属性
我有一个自引用的(树形)节点,想要根据计算出来的属性 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
,然后执行比如 subnode.name_path
,我会得到正确的结果,比如 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 逐步计算路径值了。谢谢你的帮助。
2 个回答
为了完整性,我把zzzeek在他这个链接中的反馈也加上了:https://gist.github.com/4625858
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.py、name_path和uuid_path。这些内容是用SQLAlchemy 0.8和PostgreSQL 9.2实现的。
经过一番调整PostgreSQL和SQLAlchemy,我觉得我找到了一个解决方案:(1) 首先,我需要把查询写成一个SQL函数,(2) 其次,提供正确的SQLAlchemy连接方式:
SQL部分使用了一个叫WITH RECURSIVE
的CTE:
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)
如果我在纯Python的环境下,我会避免直接访问数据库中的Node.name_path
,不过如果我这样做可能会更快。我现在唯一不太确定的是,在compile_name_path_column
中,我没有考虑到任何element, compiler, **kwargs
这些参数,这让我有点怀疑。
我是在折腾了大约1.5天的SA和PG之后才想出来这个,所以很可能还有改进的空间。如果对这个方法有任何意见,我会非常感激。谢谢。