-
-
Notifications
You must be signed in to change notification settings - Fork 278
fix: resolve nested Relation() with circular struct composition #1360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
joaquinhuigomez
wants to merge
2
commits into
uptrace:master
Choose a base branch
from
joaquinhuigomez:fix/nested-relation-struct-composition
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+106
−8
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package schema | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // Models for TestNestedRelationWithSharedComposition (issue #1243). | ||
| // These must be at package level to allow circular type references | ||
| // between Business and Lead via shared embedded relation structs. | ||
|
|
||
| type issue1243IDField struct { | ||
| ID int64 `bun:",pk,autoincrement"` | ||
| } | ||
|
|
||
| type issue1243Business struct { | ||
| issue1243IDField | ||
| issue1243LeadRelation | ||
| } | ||
|
|
||
| type issue1243BusinessRelation struct { | ||
| BusinessID int64 `bun:",nullzero"` | ||
| Business *issue1243Business `bun:"rel:belongs-to,join:business_id=id"` | ||
| } | ||
|
|
||
| type issue1243Lead struct { | ||
| issue1243IDField | ||
| issue1243BusinessRelation | ||
| } | ||
|
|
||
| type issue1243LeadRelation struct { | ||
| LeadID int64 `bun:",nullzero"` | ||
| Lead *issue1243Lead `bun:"rel:belongs-to,join:lead_id=id"` | ||
| } | ||
|
|
||
| type issue1243Agent struct { | ||
| issue1243IDField | ||
| issue1243BusinessRelation | ||
|
|
||
| Associations []*issue1243Link `bun:"rel:has-many,join:id=agent_id"` | ||
| } | ||
|
|
||
| type issue1243AgentRelation struct { | ||
| AgentID int64 `bun:",nullzero"` | ||
| Agent *issue1243Agent `bun:"rel:belongs-to,join:agent_id=id"` | ||
| } | ||
|
|
||
| type issue1243Link struct { | ||
| issue1243IDField | ||
| issue1243AgentRelation | ||
| issue1243LeadRelation | ||
| } | ||
|
|
||
| func TestNestedRelationWithSharedComposition(t *testing.T) { | ||
| dialect := newNopDialect() | ||
| tables := NewTables(dialect) | ||
|
|
||
| // Initialize Link table, which triggers circular initialization: | ||
| // Link -> AgentRelation -> Agent -> BusinessRelation -> Business | ||
| // -> LeadRelation -> Lead -> BusinessRelation -> Business (cycle) | ||
| linkTable := tables.Get(reflect.TypeFor[*issue1243Link]()) | ||
|
|
||
| require.Contains(t, linkTable.StructMap, "lead", | ||
| "Link should have 'lead' in StructMap from embedded LeadRelation") | ||
|
|
||
| leadTable := linkTable.StructMap["lead"].Table | ||
| require.Contains(t, leadTable.StructMap, "business", | ||
| "Lead should have 'business' in StructMap from embedded BusinessRelation") | ||
|
|
||
| // This is the lookup that fails in issue #1243: | ||
| // when scanning column "lead__business__id" from a nested Relation() query. | ||
| field := linkTable.LookupField("lead__business__id") | ||
| require.NotNil(t, field, | ||
| "LookupField('lead__business__id') must resolve through Lead -> Business") | ||
| } | ||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.