Skip to content

feat: add query relation#1463

Open
LinboLen wants to merge 11 commits into
goravel:masterfrom
LinboLen:feat/relation_query
Open

feat: add query relation#1463
LinboLen wants to merge 11 commits into
goravel:masterfrom
LinboLen:feat/relation_query

Conversation

@LinboLen
Copy link
Copy Markdown
Contributor

@LinboLen LinboLen commented May 4, 2026

📑 Description

Closes goravel/goravel#963

✅ Checks

  • Added test cases for my code

@LinboLen LinboLen requested a review from a team as a code owner May 4, 2026 01:37
@codecov
Copy link
Copy Markdown

codecov Bot commented May 4, 2026

Codecov Report

❌ Patch coverage is 54.55811% with 1306 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.52%. Comparing base (950dc14) to head (eda8bab).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
database/gorm/eager_loader.go 22.01% 540 Missing and 9 partials ⚠️
database/gorm/relation_writes.go 39.54% 430 Missing and 50 partials ⚠️
database/gorm/queries_relationships.go 80.03% 88 Missing and 17 partials ⚠️
database/gorm/relation.go 79.88% 44 Missing and 29 partials ⚠️
database/gorm/new_relation.go 75.37% 20 Missing and 13 partials ⚠️
database/gorm/query.go 55.17% 11 Missing and 2 partials ⚠️
database/gorm/eager_load_parse.go 89.28% 8 Missing and 4 partials ⚠️
database/orm/orm.go 0.00% 12 Missing ⚠️
database/orm/morphmap/morphmap.go 86.48% 5 Missing and 5 partials ⚠️
database/gorm/row.go 0.00% 6 Missing ⚠️
... and 3 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1463      +/-   ##
==========================================
- Coverage   69.19%   68.52%   -0.67%     
==========================================
  Files         370      385      +15     
  Lines       29338    32482    +3144     
==========================================
+ Hits        20300    22259    +1959     
- Misses       8106     9141    +1035     
- Partials      932     1082     +150     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

LinboLen added 7 commits May 11, 2026 07:30
Introduce contracts and regenerate mocks for the unified relation rework:
Relation/RelationWriter/PivotQuery, ModelWithRelations,
RelationCallback/MorphRelationCallback/PivotCallback. Add the corresponding
OrmRelation* / OrmEagerLoad* errors and remove the obsolete Association
contract.

(cherry picked from commit 1a39fa5)
Introduce a morph map under database/orm/morphmap to register and resolve
polymorphic class aliases, plus the contracts/database/orm/morph.go and
database/orm/morph.go entry points used by relation queries to translate
between *_type column values and registered model types.

(cherry picked from commit 06b7cb8)
Replace the old Preload-based With() pipeline with an eager-load engine that
parses dotted paths, batches per-relation queries, and hydrates results back
onto the parent model. Add OfMany / LatestOfMany / OldestOfMany aggregate
selectors usable inside With() callbacks. Update Cursor() to run eager loads
per-row and Load() to forward variadic args to With().

(cherry picked from commit 65dedf6)
Add the read-side relation engine: relation.go resolves Relations() definitions
into typed Has-One/Has-Many/Belongs-To/MorphMany/etc. relations with shared
key inference; new_relation.go exposes the Related() helper for ad-hoc relation
queries; queries_relationships.go provides Has, OrHas, DoesntHave, HasMorph and
WhereRelation existence/absence builders driven by Conditions.relations.

(cherry picked from commit 611d2ca)
Add the relation write API (Save/Push/Associate/Dissociate/Attach/Detach/Sync/
Toggle) implemented over the unified relation resolver, plus a PivotQuery
builder for filtering and updating pivot rows including pivot-defined
created_at/updated_at columns. Introduce contracts.db.SyncResult to report
attached/detached/updated ids per Sync call.

(cherry picked from commit ee0fcde)
…ework

Add tests covering With/Has/HasMorph/Load/PivotQuery/Sync flows against the
real database drivers, refresh tests/mock_config.go and tests go.mod/go.sum
for the new dependencies, and document the many-to-many API in
docs/orm-many-to-many.md.

(cherry picked from commit 185c0a8)
(cherry picked from commit 9f6456e)
@LinboLen LinboLen force-pushed the feat/relation_query branch from f5a4bed to da8db93 Compare May 10, 2026 23:30
@LinboLen LinboLen changed the title wip feat: add query relation feat: add query relation May 10, 2026
@hwbrzzl
Copy link
Copy Markdown
Contributor

hwbrzzl commented May 16, 2026

Hey @LinboLen Is the PR ready to be reviewed, please? And FYI, CI failed.

@LinboLen
Copy link
Copy Markdown
Contributor Author

is tested pass on my local dev environment. currently is finished state. I will fix the ci failed. this pr not use gorm associate any more. use Related for relation query, Relation for relation modify.

Comment thread database/gorm/new_relation.go
Remove GORM relation tags from test model struct fields and implement
Relations() methods instead. This aligns with the new relation system
that forbids GORM tags and requires explicit relation declarations.

Changes:
- Add `gorm:"-"` tags to all relation fields
- Implement Relations() for User, Role, Address, and Book models
- Use contractsorm relation types (HasOne, HasMany, BelongsTo, Many2Many, MorphOne, MorphMany)

Fixes CI test failures in TestWithSuite.
@LinboLen
Copy link
Copy Markdown
Contributor Author

fixed all test case. and removed association code in query. the codecov is low. adding more test case

@LinboLen LinboLen force-pushed the feat/relation_query branch from a2128fa to c01e6cf Compare May 20, 2026 07:59
@LinboLen LinboLen force-pushed the feat/relation_query branch from c01e6cf to eda8bab Compare May 20, 2026 08:01
@LinboLen
Copy link
Copy Markdown
Contributor Author

LinboLen commented May 20, 2026

some of api are real database read and write. already covered in another test case. so the codecov is down for now and can't add this coverage test

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.

feat support where has with throgh in orm

2 participants