Skip to content

Skip fields GORM ignores through the dash tag - #62

Open
dschreij wants to merge 2 commits into
go-gorm:masterfrom
dschreij:fix/ignored-fields
Open

dschreij wants to merge 2 commits into
go-gorm:masterfrom
dschreij:fix/ignored-fields

Conversation

@dschreij

@dschreij dschreij commented Sep 5, 2026

Copy link
Copy Markdown

Problem

A field tagged gorm:"-" or gorm:"-:all" maps no column, yet the generator emits a field helper for it. The helper names a column that does not exist, and a query built with it fails at the database.

Fix

isIgnoredByGORM parses the gorm tag with schema.ParseTagSetting and the generator skips fields whose - setting is - or all. A -:migration tag only skips migrations and still maps a column, so that field keeps its helper.

Test

TestIgnoredFieldsGetNoHelpers generates over a struct with a plain field, a - field, a -:all field and a -:migration field, and asserts which of them appear in the output.

The first commit adds a small test helper (generateFromSources, containsField) that writes ad hoc sources into a throwaway module and runs the generator over them. The other pull requests from this fork share that commit; git merges the identical addition without conflict, so they can land in any order.

🤖 Generated with Claude Code

Copilot AI lite review requested due to automatic review settings September 5, 2026 07:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The behavior change is small, well-targeted, and backed by a focused regression test covering the key tag variants.

Pull request overview

This PR updates the code generator to skip emitting field helpers for struct fields that GORM explicitly excludes from its schema via gorm:"-" or gorm:"-:all", preventing generated queries from referencing non-existent columns.

Changes:

  • Add isIgnoredByGORM to interpret GORM’s - tag variants using schema.ParseTagSetting.
  • Skip ignored fields during struct processing so no helper is generated for them.
  • Add test coverage to assert helpers are omitted for ignored fields and retained for -:migration.
File summaries
File Description
internal/gen/utils.go Adds isIgnoredByGORM to detect schema-ignored fields via GORM tag parsing.
internal/gen/generator.go Skips helper generation for fields ignored by GORM (- / -:all).
internal/gen/ignored_fields_test.go Adds regression test ensuring ignored fields don’t produce helpers.
internal/gen/generator_support_test.go Adds a test helper to generate code from ad-hoc sources for focused generator tests.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +19 to +23
for name, content := range files {
if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil {
t.Fatalf("write %s: %v", name, err)
}
}
@dschreij

dschreij commented Sep 5, 2026

Copy link
Copy Markdown
Author

Addressed the review. The shared test helper now creates parent directories before writing, so a nested key like models/model.go works as its doc comment promises.

@dschreij

dschreij commented Sep 5, 2026

Copy link
Copy Markdown
Author

Addressed the second round.

The shared test helper now reads the whole output tree rather than its top level, so a fixture that spans packages is not silently truncated. That was only reachable once the helper started accepting nested source paths, so thank you for following it through.

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