fix(codegen): harden PostgreSQL identifier handling in codegen#11
Merged
Conversation
Guard model code generation against empty table metadata to avoid panic and invalid output. Handle no-primary-key templates safely and replace hardcoded id ordering with parsed primary key ordering. Improve PostgreSQL parsing by supporting character varying mapping and preventing COLLATE field lines from being skipped.
Preserve prioritized id fields when generated models do not define an explicit primary key. Avoid unsafe Last fallbacks by requiring a primary key or id column for deterministic ordering. Treat PostgreSQL identity columns as non-null so generated Create methods use the correct ID type.
Restore prioritized-field ordering for generated ListByArgs queries. Keep implicit id columns ordered by descending id even when the table has no explicit primary key metadata. Add regression coverage for PostgreSQL identity columns to prevent database-dependent result ordering.
Unify model and repository generation around a safe single-identifier path. Keep implicit id behavior for legacy tables, reject unsupported composite keys, and avoid mapping PostgreSQL varchar array columns to incorrect scalar types. Add regression tests for implicit id ordering and create paths, composite-key rejection, missing identifier failures, and PostgreSQL array fallback behavior. This reduces repeated codegen regressions and keeps unsupported PostgreSQL schemas in a fail-fast state instead of generating unsafe CRUD code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Harden PostgreSQL code generation around identifier selection and unsupported schema handling.
Cause
The generator mixed explicit primary-key metadata with implicit prioritized fields.
That made different code paths disagree on the record identifier and introduced unsafe fallbacks for unsupported PostgreSQL schemas.
Solution
idbehavior for legacy and identity-backed tables in generatedCreate,Last,Updates, andListByArgsvarchar[]andcharacter varying[]columns from being generated as scalarstringfieldsTesting
go test -mod=mod ./command/codegen/codegen