Skip to content

Comments

Fix/preload composite foreign keys#7687

Open
kankankanp wants to merge 2 commits intogo-gorm:masterfrom
kankankanp:fix/preload-composite-foreign-keys
Open

Fix/preload composite foreign keys#7687
kankankanp wants to merge 2 commits intogo-gorm:masterfrom
kankankanp:fix/preload-composite-foreign-keys

Conversation

@kankankanp
Copy link
Contributor

@kankankanp kankankanp commented Jan 12, 2026

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

Fix LookUpField to 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 Name exactly matches the search name.

Changes:

  • schema/schema.go: Add name matching check in LookUpField
  • schema/schema_test.go: Add regression test TestLookUpFieldWithEmbeddedStruct

User Case Description

Using composite foreign keys with embedded structs causes Preload to fail in v1.31.1.

type SlingUser struct {
    ID    int64      `gorm:"primaryKey"`
    SrcID string     `gorm:"primaryKey"`
    User  EmbeddedUser `gorm:"embedded;embeddedPrefix:user_"`
}

type SlingShift struct {
    ID     string `gorm:"primaryKey"`
    SrcID  string `gorm:"primaryKey"`
    UserID int64
    User   *SlingUser `gorm:"foreignKey:SrcID,UserID;references:SrcID,ID"`
}

// This fails in v1.31.1, works in v1.31.0
db.Preload("User").First(&shift)
// ERROR: User is nil - Preload failed!

Fixes #7686

kankankanp and others added 2 commits January 12, 2026 18:09
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>
@kankankanp
Copy link
Contributor Author

@jinzhu
I've submitted a PR. Could you please take a look when you have a moment?

@arraytad
Copy link

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! 💚

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.

preload doesn't work in gorm 1.31.1 works in 1.31.0

2 participants