luckyframework / luckyframework/avram

Provide option to set the reverse of an association

Open
#597 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Crystal
Stars
183
Forks
67
PR merge metrics
No merged PRs in 30d

Description

It is an extremely common pattern to have hierarchical associations of this form:
```crystal
belongs_to page : Page?
has_many pages : Page
```
The main query in my application has to preload reverse associations that it should be able to simply fill in, thus extra database queries.

I would like for associations to be extended with an optional **reverse:** argument, of this form:
```crystal
belongs_to page : Page?, reverse: pages
has_many pages : Page, reverse: page
```
This would allow us to do several nice things:
* Check that the reverse association exists.
* Fill in the reverse association when creating a query result (see #389)
* Avoid an extra query to fill in the reverse association.

Contributor guide

Open the contributing guide

Research direction

No files or tests are named. Start by tracing how belongs_to and has_many declarations are handled and how query results preload associations; review #389 for the related result-loading context. Done means reverse associations can be declared, checked, populated from query results, and avoid the extra query described here.

Written by the indexing model from the issue text.

Assessment

Tech stack
crystal
Domain
database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.