Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/quiet-cte-names.md
Original file line number Diff line number Diff line change
@@ -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)
60 changes: 60 additions & 0 deletions packages/pqb/src/query/basic-features/cte/cte.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions packages/pqb/src/query/basic-features/cte/cte.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Loading