簡體   English   中英

SQLAlchemy CTE查詢引發InternalError,為什么?

[英]SQLAlchemy CTE query raises InternalError, why?

這是我試圖作為使用SQLAlchemy的應用程序的一部分運行的查詢:

WITH latest AS (
SELECT DISTINCT ON (actions.task_id) 
    actions.id AS id, 
    actions.timestamp AS timestamp, 
    actions.user_id AS user_id, 
    actions.status AS status, 
    tasks.challenge_slug AS challenge_slug, 
    actions.task_id AS task_id
FROM actions
JOIN tasks ON tasks.id = actions.task_id
ORDER BY actions.task_id DESC)
SELECT count(latest.id), latest.status
FROM latest 
GROUP BY status;

(我需要CTE中未使用的字段,以便以后進行過濾。)

直接在我的PostgreSQL數據庫上執行時,此查詢運行良好。

我使用SQLAlchemy構造對此建模如下:

latest_cte = db.session.query(
    Action.id,
    Action.task_id,
    Action.timestamp,
    Action.user_id,
    Action.status,
    Task.challenge_slug).join(
    Task).distinct(
    Action.task_id).order_by(
    Action.task_id.desc()).cte(name='latest')
tasks_query = db.session.query(
    func.count(latest_cte.c.id),
    latest_cte.c.status)

現在,當我執行:

tasks_query.all()

我收到以下錯誤消息:

sqlalchemy.exc.InternalError: (InternalError) current transaction is aborted, commands ignored until end of transaction block
'WITH latest AS \n(SELECT DISTINCT ON (actions.task_id) actions.id AS id, actions.task_id AS task_id, actions.timestamp AS timestamp, actions.user_id AS user_id, actions.status AS status, tasks.challenge_slug AS challenge_slug \nFROM actions JOIN tasks ON tasks.id = actions.task_id ORDER BY actions.task_id DESC)\n SELECT count(latest.id) AS count_1, latest.status AS latest_status \nFROM latest GROUP BY latest.status' {}

該查詢對我來說看起來相同。 這里發生了什么? 如何找出我做錯了什么?

該錯誤(可能)與您的查詢無關。 看來您在此之前已經在Shell中進行了嘗試,並且查詢失敗。 現在,您需要執行session.rollback()才能進行更多查詢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM