Skip to content

Commit 4b74ee5

Browse files
author
Christian Findlay
committed
[BUG7] Quote RETURNING clause PK column in INSERT codegen + 0.9.3-beta
PostgresCli.GenerateInsertMethod and GenerateInsertTransactionOverload hard-coded `RETURNING id` (lowercase) in the emitted INSERT SQL. For tables whose PK column is PascalCase (e.g. "Id", "PatientId") this fails at runtime with: ERROR: column "id" does not exist because Postgres lower-cases unquoted identifiers and the actual PK column name is case-sensitive (it was created with quoted ident). Fix: emit `RETURNING ""{table.PrimaryKeyColumns[0]}""` (verbatim escape) so the RETURNING clause references the real PK column with its case preserved. Falls back to `RETURNING 1` for tables with no configured primary key. Same fix applied to both the NpgsqlConnection overload and the IDbTransaction overload of Insert{Table}Async. Verified locally: regen of Clinical fhir_PatientOperations.g.cs now emits `RETURNING ""Id""`, INSERT round-trips through Postgres without 'column id does not exist' error. Bumps Directory.Build.props Version to 0.9.3-beta.
1 parent 579a935 commit 4b74ee5

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

DataProvider/DataProvider/PostgresCli.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,14 @@ string pascalName
851851
// case-folding.
852852
var colNames = string.Join(", ", insertable.Select(c => $"\"\"{c.Name}\"\""));
853853
var paramNames = string.Join(", ", insertable.Select(c => $"@{c.Name}"));
854+
// BUG7 fix: RETURNING clause must reference the actual primary-key
855+
// column name, quoted for case-folding survival. The previous hard-
856+
// coded `RETURNING id` failed at runtime on tables whose PK column is
857+
// PascalCase (e.g. "Id") with `column "id" does not exist`.
858+
var returningClause =
859+
table.PrimaryKeyColumns.Count > 0
860+
? $"RETURNING \"\"{table.PrimaryKeyColumns[0]}\"\""
861+
: "RETURNING 1";
854862

855863
_ = sb.AppendLine(" const string sql = @\"");
856864
_ = sb.AppendLine(
@@ -859,7 +867,7 @@ string pascalName
859867
);
860868
_ = sb.AppendLine(CultureInfo.InvariantCulture, $" VALUES ({paramNames})");
861869
_ = sb.AppendLine(" ON CONFLICT DO NOTHING");
862-
_ = sb.AppendLine(" RETURNING id\";");
870+
_ = sb.AppendLine(CultureInfo.InvariantCulture, $" {returningClause}\";");
863871
_ = sb.AppendLine();
864872
_ = sb.AppendLine(" try");
865873
_ = sb.AppendLine(" {");
@@ -923,6 +931,11 @@ string pascalName
923931
// as primary Insert method above.
924932
var colNames = string.Join(", ", insertable.Select(c => $"\"\"{c.Name}\"\""));
925933
var paramNames = string.Join(", ", insertable.Select(c => $"@{c.Name}"));
934+
// BUG7 fix: same RETURNING fix as the NpgsqlConnection overload.
935+
var returningClause =
936+
table.PrimaryKeyColumns.Count > 0
937+
? $"RETURNING \"\"{table.PrimaryKeyColumns[0]}\"\""
938+
: "RETURNING 1";
926939

927940
_ = sb.AppendLine();
928941
_ = sb.AppendLine(" /// <summary>");
@@ -945,7 +958,7 @@ string pascalName
945958
);
946959
_ = sb.AppendLine(CultureInfo.InvariantCulture, $" VALUES ({paramNames})");
947960
_ = sb.AppendLine(" ON CONFLICT DO NOTHING");
948-
_ = sb.AppendLine(" RETURNING id\";");
961+
_ = sb.AppendLine(CultureInfo.InvariantCulture, $" {returningClause}\";");
949962
_ = sb.AppendLine();
950963
_ = sb.AppendLine(" if (transaction.Connection is not NpgsqlConnection conn)");
951964
_ = sb.AppendLine(" {");

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<NuGetAudit>false</NuGetAudit>
55
<NuGetAuditMode>disabled</NuGetAuditMode>
66
<RestoreAuditProperties>false</RestoreAuditProperties>
7-
<Version>0.9.2-beta</Version>
7+
<Version>0.9.3-beta</Version>
88
<Authors>ChristianFindlay</Authors>
99
<Company>MelbourneDeveloper</Company>
1010
<PackageLicenseExpression>MIT</PackageLicenseExpression>

0 commit comments

Comments
 (0)