question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.related() docs are confusing or wrong

See original GitHub issue

The docs state:

The related method returns a specified relation loaded on the relations hash on the model, or calls the associated relation method and adds it to the relations hash if one exists and has not yet been loaded.

What this says to me is: “If the relations hash already has this relation loaded (.e.g .fetch({withRelated), use that one. If it has not been loaded yet, then fetch() it.”

Now, in my Express app, I have a route like:

router.get('/user/:id', auth, async (req, res, next) => {
  let id = req.params.id, user, thingies

    user = (id === 'me' || id === req.user.get('id'))
      ? req.user
      : await User.where('id', id).fetch({withRelated: ['thingies'])
    thingies = user.related('thingies')
})

When I hit the route using /user/2, for example, it works fine. But if I hit /user/me then thingies is empty. This is because earlier, in the middleware where I set req.user to be the authenticated user, I did not eager load the relation.

If I use .load() it can fix it:

user = (id === 'me') ? req.user : await User.where('id', id).fetch({withRelated: ['thingies'])
user = await user.load('thingies')
thingies = user.related('thingies')

But this loses the potential benefit of eager loading dynamically only when necessary, which is how I interpreted the docs for .related(). Am I wrong or is this a bug?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ricardogracacommented, Nov 18, 2017

Possibly related issues: #407, #711, #821 and #1018.

0reactions
DaClemenscommented, Mar 12, 2018

bump

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Python documentation is bad, and you should feel bad.
One thing that's frustrating about the documentation for me is the site's search engine. It brings up a huge list of related stuff,...
Read more >
confusing/wrong grading error message | Codecademy
I finished exercise 1.8 but not after stumbling on the following odd problem: class Person { static function say() { echo "Here are...
Read more >
Why is documentation so hard to read sometimes? - Quora
Being able to write a good and easy to read code doesn't mean that you can write a good and easy to read...
Read more >
Putting comments in code: the good, the bad, and the ugly.
For starters, there are really two different types of code comments. I call them documentation comments and clarification comments.
Read more >
Built-in Constants — Python 3.11.1 documentation
Incorrectly returning NotImplemented will result in a misleading error message or the NotImplemented value being returned to Python code.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found