Skip to content

fix(codegen): harden PostgreSQL identifier handling in codegen#11

Merged
seakee merged 4 commits into
mainfrom
fix/codegen-model-safety-and-postgres-parsing
Mar 19, 2026
Merged

fix(codegen): harden PostgreSQL identifier handling in codegen#11
seakee merged 4 commits into
mainfrom
fix/codegen-model-safety-and-postgres-parsing

Conversation

@seakee
Copy link
Copy Markdown
Owner

@seakee seakee commented Mar 19, 2026

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

  • unify model and repository generation around a safe single-identifier path
  • keep implicit id behavior for legacy and identity-backed tables in generated Create, Last, Updates, and ListByArgs
  • reject repository generation for composite primary keys and tables without a safe single identifier
  • prevent PostgreSQL varchar[] and character varying[] columns from being generated as scalar string fields
  • add regression tests and generated-code format checks for implicit ids, composite keys, missing identifiers, and PostgreSQL array fallbacks

Testing

  • go test -mod=mod ./command/codegen/codegen

seakee added 2 commits March 19, 2026 16:54
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.
@seakee seakee changed the title fix(codegen): harden model generation and PostgreSQL parsing fix(codegen): harden generated model IDs and PostgreSQL parsing Mar 19, 2026
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.
@seakee seakee changed the title fix(codegen): harden generated model IDs and PostgreSQL parsing fix(codegen): harden generated model IDs and ordering for PostgreSQL parsing Mar 19, 2026
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.
@seakee seakee changed the title fix(codegen): harden generated model IDs and ordering for PostgreSQL parsing fix(codegen): harden PostgreSQL identifier handling in codegen Mar 19, 2026
@seakee seakee merged commit 3d73c7d into main Mar 19, 2026
@seakee seakee deleted the fix/codegen-model-safety-and-postgres-parsing branch March 25, 2026 11:46
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.

1 participant