Fix/preload composite foreign keys#7687
Open
kankankanp wants to merge 2 commits intogo-gorm:masterfrom
Open
Conversation
When using composite foreign keys with embedded fields, LookUpField would incorrectly return embedded fields with matching DBNames. For example, searching for "UserID" in a schema with an embedded User struct would return the embedded "ID" field (DBName: "user_id") instead of returning nil and allowing the correct field resolution to occur. This fix ensures the namer-based lookup only returns a field if its Name exactly matches the search name, preventing mismatches between field names and their database column names. Fixes go-gorm#7686 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add test case to ensure LookUpField returns the correct field when searching for a field name that exists in the schema, and returns nil when searching for a non-existent field name (rather than returning an embedded field with a matching DBName). This test covers the bug fixed for issue go-gorm#7686. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Contributor
Author
|
@jinzhu |
|
I can confirm that building against this PR, in addition to fixing the testcase from #7686 also fixes my issue in the production codebase that I extracted the testcase from. Thank you for such a fast review + fix! 💚 |
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.
What did this pull request do?
Fix
LookUpFieldto prevent returning wrong embedded fields when using namer-based column lookup.When searching for a field like "UserID" in a schema with embedded structs, the namer-based lookup (added in #7619) would incorrectly return embedded fields with matching DBNames. For example, searching for "UserID" would return an embedded "ID" field with DBName "user_id" instead of returning nil.
This fix adds a check to ensure the namer-based lookup only returns a field if its
Nameexactly matches the search name.Changes:
schema/schema.go: Add name matching check inLookUpFieldschema/schema_test.go: Add regression testTestLookUpFieldWithEmbeddedStructUser Case Description
Using composite foreign keys with embedded structs causes Preload to fail in v1.31.1.
Fixes #7686