我试图在SQLAlchemy中实现以下查询来处理嵌套集(请参阅here)。我正在努力解决的问题是如何在depth
子句中的主SELECT
查询(取决于子SELECT
查询)中使用带标签的HAVING
计算最后。
SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth
FROM nested_category AS node,
nested_category AS parent,
nested_category AS sub_parent,
(
SELECT node.name, (COUNT(parent.name) - 1) AS depth
FROM nested_category AS node,
nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.name = 'PORTABLE ELECTRONICS'
GROUP BY node.name
ORDER BY node.lft
)AS sub_tree
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
AND sub_parent.name = sub_tree.name
GROUP BY node.name
HAVING depth <= 1
ORDER BY node.lft;
我觉得我在使用时非常接近:
node = aliased(Category)
parent = aliased(Category)
sub_parent = aliased(Category)
sub_tree = DBSession.query(node.name,
(func.count(parent.name) - 1).label('depth')).\
filter(node.lft.between(parent.lft, parent.rgt)).\
filter(node.name == category_name).\
group_by(node.name).\
order_by(node.lft).subquery()
children = DBSession.query(node.name,
(func.count(parent.name) - (sub_tree.c.depth + 1)).label('depth')).\
filter(node.lft.between(parent.lft, parent.rgt)).\
filter(node.lft.between(sub_parent.lft, sub_parent.rgt)).\
filter(sub_parent.name == sub_tree.c.name).\
group_by(node.name).having(depth <= 1).\
order_by(node.lft).all()
但是,我最终得到了错误:
NameError: global name 'depth' is not defined
哪种有道理。如果我用having(depth <= 1)
替换having(func.count('depth') <= 1
,我最终会生成以下HAVING
子句,该子句不返回任何结果(%s占位符在哪里(&#39;深度&#39;,1) ):
HAVING count(%s) <= %s
我真正需要阅读的内容完全如下:
HAVING depth = 1
有人有什么想法吗?
我最后的办法是实际执行原始查询而不是通过ORM层,但我真的不愿意,因为我离这么近......
提前致谢。
编辑:
我还尝试了以下代码,但它没有返回正确的结果(就好像&#39; depth&#39;标签始终为0):
node = aliased(Category)
parent = aliased(Category)
sub_parent = aliased(Category)
sub_tree_depth = (func.count(parent.name) - 1).label('depth')
depth = (func.count(parent.name) - (sub_tree_depth + 1)).label('depth')
sub_tree = DBSession.query(node.name,
sub_tree_depth).\
filter(node.lft.between(parent.lft, parent.rgt)).\
filter(node.name == category_name).\
group_by(node.name).\
order_by(node.lft).subquery()
children = DBSession.query(node.name,
depth).\
filter(node.lft.between(parent.lft, parent.rgt)).\
filter(node.lft.between(sub_parent.lft, sub_parent.rgt)).\
filter(sub_parent.name == sub_tree.c.name).\
group_by(node.name).having(depth <= 1).\
order_by(node.lft).all()
从此生成的HAVING
子句看起来像(来自原始查询中的categories_2 == parent):
HAVING count(categories_2.name) - ((count(categories_2.name) - 1) + 1) <= 1
编辑:
我认为包含生成的SQL可能会有所帮助。
SQLAlchemy的
node = aliased(Category)
parent = aliased(Category)
sub_parent = aliased(Category)
sub_tree = DBSession.query(node.name,
(func.count(parent.name) - 1).label('depth')).\
filter(node.lft.between(parent.lft, parent.rgt)).\
filter(node.name == category_name).\
group_by(node.name).\
order_by(node.lft).subquery()
depth = (func.count(parent.name) - (sub_tree.c.depth + 1)).label('depth')
children = DBSession.query(node.name, depth).\
filter(node.lft.between(parent.lft, parent.rgt)).\
filter(node.lft.between(sub_parent.lft, sub_parent.rgt)).\
filter(sub_parent.name == sub_tree.c.name).\
group_by(node.name).having(depth <= 1).\
order_by(node.lft)
生成SQL
'SELECT categories_1.name AS categories_1_name, count(categories_2.name) - (anon_1.depth + %s) AS depth
FROM categories AS categories_1, categories AS categories_2, (SELECT categories_1.name AS name, count(categories_2.name) - %s AS depth
FROM categories AS categories_1, categories AS categories_2
WHERE categories_1.lft BETWEEN categories_2.lft AND categories_2.rgt AND categories_1.name = %s GROUP BY categories_1.name ORDER BY categories_1.lft) AS anon_1, categories AS categories_3
WHERE categories_1.lft BETWEEN categories_2.lft AND categories_2.rgt AND categories_1.lft BETWEEN categories_3.lft AND categories_3.rgt AND categories_3.name = anon_1.name GROUP BY categories_1.name
HAVING count(categories_2.name) - (anon_1.depth + %s) <= %s ORDER BY categories_1.lft' (1, 1, u'Institutional', 1, 1)
答案 0 :(得分:7)
您的SQL查询使用隐式连接,在SQLAlchemy中您需要显式定义它们。除此之外你的第二次尝试几乎是正确的:
node = aliased(Category)
parent = aliased(Category)
sub_parent = aliased(Category)
sub_tree = DBSession.query(node.name,
(func.count(parent.name) - 1).label('depth')).\
join(parent, node.lft.between(parent.lft, parent.rgt)).\
filter(node.name == category_name).\
group_by(node.name).\
order_by(node.lft).subquery()
depth = (func.count(parent.name) - (sub_tree.c.depth + 1)).label('depth')
children = DBSession.query(node.name, depth).\
join(parent, node.lft.between(parent.lft, parent.rgt)).\
join(sub_parent, node.lft.between(sub_parent.lft, sub_parent.rgt)).\
join(sub_tree, sub_parent.name == sub_tree.c.name).\
group_by(node.name, sub_tree.c.depth).\
having(depth <= 1).\
order_by(node.lft).all()
如果生成的SQL中的HAVING
子句将重复完整表达式而不是其别名,请不要感到惊讶。那是因为那里不允许别名,它只是MySQL扩展,而SQLAlchemy努力生成在大多数情况下都有效的SQL。