Load nested relation


#1

When I’m try load nested relations I’m received throw:

E_CANNOT_OVERRIDE_RELATION: Trying to eagerload changed_company relationship twice

const sim = await Sim.findOrFail(params.id)

await Promise.all([
  sim.load('changed_company.user'),
  sim.load('changed_company.old_company')
])

How to load this relations?


#2

Want to try

const sim = await Sim.findOrFail(params.id)
sim.loadMany({
   'changed_company.user': null,
   'changed_company.old_company': null
})

You can also define a function instead of null to add more query constraints


#3

This is what I’m waiting for to solve the problem from https://github.com/adonisjs/adonis-lucid/issues/169
Need documented! :slight_smile:


#4

Yup docs needs some love


#5

It’s see better If I can merge codes

const sim = await Sim.query()
  .with('changed_company')
  .with('changed_company.user')
  .with('changed_company.old_company')
  .where('id', params.id)
  .first()
const sim = await Sim.findOrFail(params.id)

And finally is better view as:

const sim = await Sim.query()
  .with('changed_company')
  .with('changed_company.user')
  .with('changed_company.old_company')
  .findOrFail(params.id)

But findOrFail is belongs to a model not query builder.


#6

Nice! Thanks!