From ccd8fb2eb1eab970acee237c5ea99aef102b0bd3 Mon Sep 17 00:00:00 2001 From: Ilya Semenov Date: Tue, 17 Nov 2026 12:59:09 +0700 Subject: [PATCH] Fix withSql referencing named shape columns by name (#903) --- .changeset/quiet-cte-names.md | 6 ++ .../basic-features/cte/cte.query.test.ts | 60 +++++++++++++++++++ .../src/query/basic-features/cte/cte.query.ts | 13 +++- 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 .changeset/quiet-cte-names.md diff --git a/.changeset/quiet-cte-names.md b/.changeset/quiet-cte-names.md new file mode 100644 index 000000000..fa1e3609f --- /dev/null +++ b/.changeset/quiet-cte-names.md @@ -0,0 +1,6 @@ +--- +'pqb': patch +'orchid-orm': patch +--- + +Fix `withSql` referencing a shape column with `name()` by its name instead of the key the CTE declares (#903) diff --git a/packages/pqb/src/query/basic-features/cte/cte.query.test.ts b/packages/pqb/src/query/basic-features/cte/cte.query.test.ts index 570eec061..ba85fe5cd 100644 --- a/packages/pqb/src/query/basic-features/cte/cte.query.test.ts +++ b/packages/pqb/src/query/basic-features/cte/cte.query.test.ts @@ -291,6 +291,66 @@ describe('withSql', () => { expect(res).toEqual([{ one: 1, two: 'two' }]); }); + describe('named column', () => { + const withNamed = () => + db.user.withSql( + 'w', + (t) => ({ + authorId: t.name('author_id').text().parse(parseInt), + points: t.integer(), + }), + () => sql`SELECT '1', 10`, + ); + + it('should reference a named column by its key', async () => { + const q = withNamed() + .from('w') + .select('authorId', 'points') + .where({ authorId: '1' }); + + expectSql( + q.toSQL(), + ` + WITH "w"("authorId", "points") AS (SELECT '1', 10) + SELECT "w"."authorId", "w"."points" + FROM "w" + WHERE "w"."authorId" = $1 + `, + ['1'], + ); + + expect(await q).toEqual([{ authorId: 1, points: 10 }]); + }); + + it('should return and parse a named column by its key when selecting all', async () => { + const res = await withNamed().from('w'); + + expect(res).toEqual([{ authorId: 1, points: 10 }]); + }); + + it('should expose a named column by its key to another CTE', async () => { + const res = await withNamed() + .with('v', (q) => q.from('w')) + .from('v') + .select('authorId'); + + expect(res).toEqual([{ authorId: 1 }]); + }); + + it('should not change the shape returned from the shape callback', () => { + db.user.withSql( + 'w', + () => db.user.shape, + () => sql`SELECT * FROM "schema"."user"`, + ); + + expectSql( + db.user.select('UserKey').toSQL(), + `SELECT "User"."user_key" "UserKey" FROM "schema"."user" "User"`, + ); + }); + }); + it('should support all with options', () => { for (const { options: opts, sql: s } of makeOptions('*', ['id', 'name'])) { const q = db.user diff --git a/packages/pqb/src/query/basic-features/cte/cte.query.ts b/packages/pqb/src/query/basic-features/cte/cte.query.ts index 2402fd709..5036adbcf 100644 --- a/packages/pqb/src/query/basic-features/cte/cte.query.ts +++ b/packages/pqb/src/query/basic-features/cte/cte.query.ts @@ -2,7 +2,7 @@ import { Query } from '../../query'; import { setQueryObjectValueImmutable } from '../../query.utils'; import { _queryUnion } from '../union/union'; import { CteItem, CteOptions } from './cte.sql'; -import { Column } from '../../../columns'; +import { Column, setColumnData } from '../../../columns'; import { prepareSubQueryForSql } from '../../internal-features/sub-query/sub-query-for-sql'; import { PickQueryResult, @@ -506,7 +506,16 @@ export class CteQuery { const [options, shapeFn, sql] = args.length === 2 ? [undefined, args[0], args[1]] : args; - const shape = shapeFn(this.columnTypes); + const shape = { ...shapeFn(this.columnTypes) }; + + // the CTE declares its columns by the shape keys, + // so references to them must not use `name()` + for (const key in shape) { + const column = shape[key]; + if (column.data.name) { + shape[key] = setColumnData(column, 'name', undefined); + } + } pushQueryValueImmutable(q, 'with', { n: name,