我有以下自引用(树)节点,并希望按计算的属性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迭代计算路径值。谢谢你的帮助。
答案 0 :(得分:0)
在使用PostgreSQL和SQLAlchemy进行调整之后,我认为我有一个解决方案:(1)首先,我将查询作为SQL中的函数编写,然后(2)第二次提供正确的SQLAlchemy粘合剂:< / p>
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
参数,这让我有点怀疑。
我用SA&amp; amp;修补了大约1.5天后,我就把它煮熟了。 PG,所以很有可能还有改进的余地。我非常感谢w.r.t的任何言论。这种方法。感谢。
答案 1 :(得分: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.py,name_path和uuid_path找到。这个东西已经用SQLAlchemy 0.8和PostgreSQL 9.2实现。