SQLAlchemy中带有子句的递归CTE

时间:2018-01-29 00:24:07

标签: python sqlalchemy common-table-expression

我正在尝试在SQLAlchemy中实现CTE,它从父/子树中的给定节点返回根节点。

我能够按照Recursive CTE Query中的示例在SQLAlchemy中生成CTE递归。

但我不确定如何在其上添加附加条件。即:

WITH ...
SELECT parent_id from parents
WHERE parent_id NOT IN (SELECT child_id FROM node_parent_relation );

我想这样做,这样我才能重新获得根节点。

该查询适用于SQL。

WITH RECURSIVE parents( child_id, parent_id ) 
AS (
  -- get leaf children
  SELECT child_id, parent_id
  FROM node_parent_relation
  WHERE child_id = '4415b581-0a31-43e8-a69d-d74caeccacd2'

  UNION ALL

  -- get all parents  
  SELECT t.child_id, t.parent_id
  FROM parents p
  JOIN node_parent_relation t
  ON p.parent_id = t.child_id
)
SELECT parent_id from parents
WHERE parent_id NOT IN (SELECT child_id FROM node_parent_relation );

但是我可以看到如何将这个附加子句添加到我的SQLAlchemy语句中。

我想这会涉及到某处添加这些陈述。

sub_query = db.session.query(NodeParentRelation.child_id)
[...].filter([...].parent_id.in_(sub_query))

这就是我的SQLAlchemy代码目前的样子:

parent = aliased(NodeParentRelation)
child = aliased(NodeParentRelation)

sub_query = db.session.query(NodeParentRelation.child_id)
top_query = db.session.query(NodeParentRelation.child_id,
                             NodeParentRelation.parent_id)\
                         .filter(NodeParentRelation.child_id == self.id)\
                         .cte(recursive=True)

bottom_query = db.session.query(child.child_id, child.parent_id)\
                         .join(parent, parent.parent_id == child.child_id)

query = top_query.union(bottom_query)

作为参考,这是当前从上述SQLAlchemy语句构造的SQL查询。

WITH RECURSIVE anon_1(child_id, parent_id) AS
(SELECT node_parent_relation.child_id AS child_id, node_parent_relation.parent_id AS parent_id
FROM node_parent_relation
WHERE node_parent_relation.child_id = %(child_id_1)s UNION SELECT node_parent_relation_1.child_id AS node_parent_relation_1_child_id, node_parent_relation_1.parent_id AS node_parent_relation_1_parent_id
FROM node_parent_relation AS node_parent_relation_1 JOIN node_parent_relation AS node_parent_relation_2 ON node_parent_relation_2.parent_id = node_parent_relation_1.child_id)
 SELECT anon_1.child_id AS anon_1_child_id, anon_1.parent_id AS anon_1_parent_id
FROM anon_1

1 个答案:

答案 0 :(得分:3)

当前查询根本不会在递归步骤中引用CTE。您需要CTE本身的别名才能解决这个问题:

In [5]: top_query = db.session.query(NodeParentRelation.child_id,
   ...:                              NodeParentRelation.parent_id)\
   ...:                          .filter(NodeParentRelation.child_id == 1)\
   ...:                          .cte(recursive=True)
   ...:                              

In [6]: parents = db.aliased(top_query)

In [7]: t = db.aliased(NodeParentRelation)

In [11]: query = top_query.union_all(
    ...:     db.session.query(t).
    ...:         join(parents, t.child_id == parents.c.parent_id))

其余的都很标准:

In [14]: db.session.query(query.c.parent_id).\
    ...:     filter(query.c.parent_id.notin_(
    ...:         db.session.query(NodeParentRelation.child_id)))
Out[14]: <sqlalchemy.orm.query.Query at 0x7f56bf55b668>

In [15]: print(_)
WITH RECURSIVE anon_1(child_id, parent_id) AS 
(SELECT node_parent_relation.child_id AS child_id, node_parent_relation.parent_id AS parent_id 
FROM node_parent_relation 
WHERE node_parent_relation.child_id = ? UNION ALL SELECT node_parent_relation_1.parent_id AS node_parent_relation_1_parent_id, node_parent_relation_1.child_id AS node_parent_relation_1_child_id 
FROM node_parent_relation AS node_parent_relation_1 JOIN anon_1 AS anon_2 ON node_parent_relation_1.child_id = anon_2.parent_id)
 SELECT anon_1.parent_id AS anon_1_parent_id 
FROM anon_1 
WHERE anon_1.parent_id NOT IN (SELECT node_parent_relation.child_id AS node_parent_relation_child_id 
FROM node_parent_relation)

虽然我个人不喜欢不存在:

In [25]: db.session.query(query.c.parent_id).\
    ...:     filter(~db.session.query(NodeParentRelation).
    ...:         filter(NodeParentRelation.child_id == query.c.parent_id).
    ...:         exists())
    ...: