Skip to content

Commit 5d90bb3

Browse files
authored
fix: apply column overrides correctly with aliased columns in RETURNING clause (#428)
Fixes an issue where column overrides were not applied correctly when using aliased columns in the RETURNING clause of an INSERT statement. The fix ensures that the correct column name is used to look up overrides, supporting custom types for aliased returning columns.
1 parent 7be764d commit 5d90bb3

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

.changeset/fresh-beers-open.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@ts-safeql/generate": patch
3+
---
4+
5+
fix: apply column overrides correctly when using aliased columns in RETURNING clause
6+

packages/generate/src/generate-insert.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,4 +493,18 @@ describe("INSERT validation", () => {
493493
unknownColumns: ["col"],
494494
});
495495
});
496+
497+
test("INSERT with column override and aliased returning should use custom type", async () => {
498+
await testQuery({
499+
options: {
500+
overrides: {
501+
columns: { "test_tbl.col": "CustomType" },
502+
},
503+
},
504+
schema: `CREATE TABLE test_tbl (col TEXT NOT NULL);`,
505+
query: `INSERT INTO test_tbl (col) VALUES ('val') RETURNING col AS aliased_col`,
506+
expected: [["aliased_col", { kind: "type", value: "CustomType", type: "text" }]],
507+
unknownColumns: ["aliased_col"],
508+
});
509+
});
496510
});

packages/generate/src/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ function getResolvedTargetEntry(params: {
481481

482482
const columnOverride = params?.context?.overrides?.columns
483483
.get(params.col.introspected?.tableName ?? "")
484-
?.get(params.col.described.name);
484+
?.get(params.col.introspected?.colName ?? params.col.described.name);
485485

486486
if (columnOverride !== undefined) {
487487
return { kind: "type", value: columnOverride, type: pgType.name } satisfies ResolvedTarget;

0 commit comments

Comments
 (0)