Skip to content

Quote column names in DuplicatedField update fragments#4237

Closed
MarkVDD wants to merge 2 commits intoJasperFx:masterfrom
MarkVDD:fix/duplicated-field-reserved-keywords
Closed

Quote column names in DuplicatedField update fragments#4237
MarkVDD wants to merge 2 commits intoJasperFx:masterfrom
MarkVDD:fix/duplicated-field-reserved-keywords

Conversation

@MarkVDD
Copy link
Copy Markdown
Contributor

@MarkVDD MarkVDD commented Apr 7, 2026

Description

The Problem

When adding a new DuplicatedField (mapping a JSONB property to a dedicated column) where the property name is a PostgreSQL reserved keyword (e.g., Offset), Marten fails during the auto-migration phase. While the ALTER TABLE might succeed, the subsequent bulk update to populate the new column fails:

Failing SQL:
update public.mt_doc_foo set offset = CAST(data ->> 'Offset' as bigint); -- Syntax error

The Fix

Modified DuplicatedField.UpdateSqlFragment() to unconditionally quote the ColumnName. Since PostgreSQL is case-insensitive for unquoted identifiers but case-sensitive for quoted ones, and Marten already ensures column names are lowercase, always quoting is the safest and most performant way to handle all potential keywords.

Changes

  • Core: Updated Marten.Schema.DuplicatedField.UpdateSqlFragment() to wrap the column name in double quotes.

Dependency

This PR is complementary to the fix in Weasel (JasperFx/weasel#242) which handles the quoting within the generated Upsert functions. Together, these changes provide full support for reserved keywords in document properties.

Verification

  • Verified via the updated Weasel test suite that ToFunctionUpdate now produces correctly quoted identifiers for reserved keywords.
  • Confirmed that DuplicatedField now generates quoted column identifiers for bulk data migration, matching the schema requirements for reserved keywords.
  • Verified that this change does not affect existing tests as Marten-generated column names are already normalized to lowercase.

@jeremydmiller
Copy link
Copy Markdown
Member

@MarkVDD I've got the Weasel changes in. I'm working on the regression failures up above. Thanks for taking this on!

jeremydmiller added a commit that referenced this pull request Apr 8, 2026
…agment

Update all test expectations to include double-quoted column names,
matching the change in DuplicatedField.UpdateSqlFragment() from PR #4237
that unconditionally quotes column names to handle PostgreSQL reserved
keywords (e.g., "offset").

26/26 DuplicatedFieldTests passing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@MarkVDD
Copy link
Copy Markdown
Contributor Author

MarkVDD commented Apr 8, 2026

@MarkVDD I've got the Weasel changes in. I'm working on the regression failures up above. Thanks for taking this on!

All good. Just let me know if I can help ;)

jeremydmiller added a commit that referenced this pull request Apr 8, 2026
…agment

Update all test expectations to include double-quoted column names,
matching the change in DuplicatedField.UpdateSqlFragment() from PR #4237
that unconditionally quotes column names to handle PostgreSQL reserved
keywords (e.g., "offset").

26/26 DuplicatedFieldTests passing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@jeremydmiller
Copy link
Copy Markdown
Member

@MarkVDD I pulled your work in through a separate pull request w/ some annoying test fixes to go with your real fixes. Thank you!

The Weasel release is coming too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants