fix: SQL语法错误—func.and_()/or_()改为sqlalchemy.and_()/or_()
This commit is contained in:
@ -420,15 +420,15 @@ async def get_all_products(
|
|||||||
overall_names: dict[uuid.UUID, str] = {}
|
overall_names: dict[uuid.UUID, str] = {}
|
||||||
main_assignees: dict[uuid.UUID, str] = {}
|
main_assignees: dict[uuid.UUID, str] = {}
|
||||||
if product_ids:
|
if product_ids:
|
||||||
from sqlalchemy import func as sa_func2, case as sa_case2
|
from sqlalchemy import and_, or_, func as sa_func, case as sa_case
|
||||||
main_where = sa_func2.and_(
|
main_where = and_(
|
||||||
Task.product_id.in_(product_ids),
|
Task.product_id.in_(product_ids),
|
||||||
sa_func2.or_(
|
or_(
|
||||||
Task.parent_task_id.is_(None),
|
Task.parent_task_id.is_(None),
|
||||||
Task.task_type.in_(["TRANSFER", "RECOVERY"]),
|
Task.task_type.in_(["TRANSFER", "RECOVERY"]),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
prio_expr = sa_case2(
|
prio_expr = sa_case(
|
||||||
(Task.status == "WIP", 3),
|
(Task.status == "WIP", 3),
|
||||||
(Task.status == "PENDING", 2),
|
(Task.status == "PENDING", 2),
|
||||||
(Task.status == "COMPLETED", 1),
|
(Task.status == "COMPLETED", 1),
|
||||||
@ -436,20 +436,20 @@ async def get_all_products(
|
|||||||
)
|
)
|
||||||
# 子查询:每个产品最高优先级主干任务
|
# 子查询:每个产品最高优先级主干任务
|
||||||
max_prio = (
|
max_prio = (
|
||||||
select(Task.product_id, sa_func2.max(prio_expr).label("prio"))
|
select(Task.product_id, sa_func.max(prio_expr).label("prio"))
|
||||||
.where(main_where)
|
.where(main_where)
|
||||||
.group_by(Task.product_id)
|
.group_by(Task.product_id)
|
||||||
).subquery("mp")
|
).subquery("mp")
|
||||||
# JOIN 回 tasks 拿 task_name + assignee_id(同优先级取最新创建的)
|
# JOIN 回 tasks 拿 task_name + assignee_id(同优先级取最新创建的)
|
||||||
main_stmt = (
|
main_stmt = (
|
||||||
select(Task.product_id, Task.task_name, Task.assignee_id)
|
select(Task.product_id, Task.task_name, Task.assignee_id)
|
||||||
.join(max_prio, sa_func2.and_(
|
.join(max_prio, and_(
|
||||||
Task.product_id == max_prio.c.product_id,
|
Task.product_id == max_prio.c.product_id,
|
||||||
prio_expr == max_prio.c.prio,
|
prio_expr == max_prio.c.prio,
|
||||||
))
|
))
|
||||||
.where(main_where)
|
.where(main_where)
|
||||||
.order_by(Task.product_id, Task.created_at.desc())
|
.order_by(Task.product_id, Task.created_at.desc())
|
||||||
.distinct(Task.product_id) # PostgreSQL DISTINCT ON
|
.distinct(Task.product_id)
|
||||||
)
|
)
|
||||||
main_result = await db.execute(main_stmt)
|
main_result = await db.execute(main_stmt)
|
||||||
for row in main_result:
|
for row in main_result:
|
||||||
|
|||||||
Reference in New Issue
Block a user