Skip to content

fix: resolve nested Relation() with circular struct composition#1360

Open
joaquinhuigomez wants to merge 2 commits into
uptrace:masterfrom
joaquinhuigomez:fix/nested-relation-struct-composition
Open

fix: resolve nested Relation() with circular struct composition#1360
joaquinhuigomez wants to merge 2 commits into
uptrace:masterfrom
joaquinhuigomez:fix/nested-relation-struct-composition

Conversation

@joaquinhuigomez
Copy link
Copy Markdown

When models use shared embedded relation structs that form circular dependencies (e.g. Business -> LeadRelation -> Lead -> BusinessRelation -> Business), LookupField fails for nested columns like lead__business__id because StructMap entries are missing due to initialization order.

The fix splits struct field registration into a Placeholder step (registers the StructMap entry with the table pointer) before triggering recursive InProgress initialization. This ensures StructMap entries are visible to types in the same circular dependency chain during their own processFields.

Fixes #1243

Copy link
Copy Markdown
Collaborator

@Aoang Aoang left a comment

Choose a reason for hiding this comment

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

Thank you for this PR! The fix looks solid.

@Aoang Aoang requested a review from j2gg0s April 2, 2026 03:15
Comment thread schema/issue1243_test.go Outdated
Comment thread schema/tables.go
@joaquinhuigomez
Copy link
Copy Markdown
Author

@bevzzz thanks for the review. Pushed:

  • Dropped the issue1243- prefix from the struct names — agreed it reads cleaner with the file/docstring as context.
  • Moved the initStarted guard into Table.init() so callers don't have to manage it.
  • Refactored InProgress to delegate to Placeholder + init, matching the structure you suggested.

TestNestedRelationWithSharedComposition still passes.

@bevzzz
Copy link
Copy Markdown
Collaborator

bevzzz commented May 4, 2026

great @joaquinhuigomez, thank you!
you'll need to rewrite the last commit message as it's failing the commit-lint rn, but otherwise we're good to go

refactor(schema): address review feedback

[details]

- Drop the issue1243- prefix from struct names in issue1243_test.go
  since the file name and docstring already provide context.
- Move the initStarted guard into Table.init() so callers no longer
  have to set or check it.
- Refactor InProgress to delegate to Placeholder + init, removing the
  duplicated lookup and insert logic.

TestNestedRelationWithSharedComposition still passes.
@joaquinhuigomez joaquinhuigomez force-pushed the fix/nested-relation-struct-composition branch from 84830a8 to e7a7efe Compare May 4, 2026 13:00
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.

Regression in v1.2.X: Nested Relation() fails when using struct composition for shared fields and relations

3 participants