在 sqlalchemy 中的有序子查询上加入 DISTINCT ON

joining with a DISTINCT ON on an ordered subquery in sqlalchemy

这是我的问题(极其简化的版本)。

我正在使用 Postgresql 作为后端并尝试构建一个 sqlalchemy 查询 来自另一个查询。

Table 设置

这里是带有一些随机数据的 table 示例。 您可以假设每个 table 都是在 sqlalchemy 中以声明方式声明的,其中 映射器的名称分别为 Item 和 ItemVersion。 在问题的末尾,你可以找到一个 link ,我把代码放在那里 这个问题的所有内容,包括 table 定义。

一些项目。

item
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
+----+

A table 包含每个项目的版本。每个人至少有一个。

item_version
+----+---------+---------+-----------+
| id | item_id | version | text      |
+----+---------+---------+-----------+
|  1 |       1 |       0 | item_1_v0 |
|  2 |       1 |       1 | item_1_v1 |
|  3 |       2 |       0 | item_2_v0 |
|  4 |       3 |       0 | item_3_v0 |
+----+---------+---------+-----------+

查询

现在,对于 Item 上的给定 sqlalchemy 查询,我想要一个 returns 的函数 另一个查询,但这次是 (Item, ItemVersion),其中 Item 是 与原始查询中的相同(并且顺序相同!),并且 ItemVersion是每个Item对应的最新版本。

下面是 SQL 中的一个例子,非常简单:

首先对 item table

进行随机查询
SELECT item.id as item_id
FROM item
WHERE item.id != 2
ORDER BY item.id DESC

对应

+---------+
| item_id |
+---------+
|       3 |
|       1 |
+---------+

然后从那个查询,如果我想加入正确的 versions,我可以做

SELECT sq2.item_id AS item_id,
       sq2.item_version_id AS item_version_id,
       sq2.item_version_text AS item_version_text
FROM (
    SELECT DISTINCT ON (sq.item_id)
           sq.item_id AS item_id,
           iv.id AS item_version_id,
           iv.text AS item_version_text
    FROM (
        SELECT item.id AS item_id
        FROM item
        WHERE id != 2
        ORDER BY id DESC) AS sq
    JOIN item_version AS iv
      ON iv.item_id = sq.item_id
    ORDER BY sq.item_id, iv.version DESC) AS sq2
ORDER BY sq2.item_id DESC

请注意,它必须第二次包装在子查询中,因为 DISTINCT ON 放弃排序。

现在的挑战是在 sqlalchemy 中编写一个执行此操作的函数。 这是我目前所拥有的。

首先对项目进行初始 sqlalchemy 查询:

session.query(Item).filter(Item.id != 2).order_by(desc(Item.id))

然后我可以构建我的第二个查询,但是没有原始顺序。在 换句话说,我不知道如何进行我在其中所做的第二个子查询包装 SQL 取回被 DISTINCT ON.

丢弃的顺序
def join_version(session, query):
    sq = aliased(Item, query.subquery('sq'))

    sq2 = session.query(sq, ItemVersion) \
        .distinct(sq.id) \
        .join(ItemVersion) \
        .order_by(sq.id, desc(ItemVersion.version))
    return sq2

我认为 this SO question 可能是答案的一部分,但我不完全是 确定如何。

运行 这个问题中所有内容的代码(数据库创建、填充和 到目前为止,我的单元测试失败了)can be found here。通常情况下 如果你能修复 join_version 功能,它应该使测试通过!

好的,所以我找到了一个方法。这有点 hack,但仍然只查询数据库两次,所以我想我会活下来!基本上,我首先在数据库中查询 Item,然后再查询 ItemVersion,过滤 item_id,然后过滤 reordering with a trick I found here (this is also relevant).

代码如下:

def join_version(session, query):                                  
    items = query.all()                                            
    item_ids = [i.id for i in items]                               
    items_v_sq = session.query(ItemVersion) \                      
        .distinct(ItemVersion.item_id) \                           
        .filter(ItemVersion.item_id.in_(item_ids)) \               
        .order_by(ItemVersion.item_id, desc(ItemVersion.version)) \
        .subquery('sq')                                            
    sq = aliased(ItemVersion, items_v_sq)                          
    items_v = session.query(sq) \                                  
        .order_by('idx(array{}, sq.item_id)'.format(item_ids))     
                                                                   
    return zip(items, items_v)