fix(api): fix batch user-files delete recursive CTE query

The refactored CTE used = ANY(${validIds}::uuid[]) which Drizzle
serializes as a scalar text parameter with a ::uuid[] cast. Since
the user_files.id column is text (not uuid), PostgreSQL rejects
the text = uuid comparison. Replace with IN (...) via sql.join()
to pass each ID as a plain text parameter matching the column type.
This commit is contained in:
SnapOtter
2026-06-13 10:55:14 +08:00
parent e2c57144dd
commit 82991b6a41
+5 -1
View File
@@ -505,11 +505,15 @@ export async function userFileRoutes(app: FastifyInstance): Promise<void> {
}; };
// Single recursive CTE to collect all chain members for every valid ID // Single recursive CTE to collect all chain members for every valid ID
const seedIds = sql.join(
validIds.map((id) => sql`${id}`),
sql`, `,
);
const cteResult = await db.execute<DeleteChainRow>(sql` const cteResult = await db.execute<DeleteChainRow>(sql`
WITH RECURSIVE WITH RECURSIVE
ancestors(id, parent_id) AS ( ancestors(id, parent_id) AS (
SELECT id, parent_id FROM user_files SELECT id, parent_id FROM user_files
WHERE id = ANY(${validIds}::uuid[]) WHERE id IN (${seedIds})
UNION ALL UNION ALL
SELECT uf.id, uf.parent_id FROM user_files uf SELECT uf.id, uf.parent_id FROM user_files uf
INNER JOIN ancestors a ON uf.id = a.parent_id INNER JOIN ancestors a ON uf.id = a.parent_id