Skip to content

Commit

Permalink
Merge d713f01 into 5703a7c
Browse files Browse the repository at this point in the history
  • Loading branch information
radmen authored Jan 4, 2018
2 parents 5703a7c + d713f01 commit c8cc658
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Lucid/EagerLoad/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ class EagerLoad {
* @private
*/
_chainNested ({ relatedQuery }, nested) {
if (nested) {
const name = _.first(_.keys(nested))
relatedQuery.with(name, nested[name])
}
Object.entries(nested || {})
.forEach(([name, callback]) => {
relatedQuery.with(name, callback)
})
}

/**
Expand Down
39 changes: 39 additions & 0 deletions test/unit/lucid-relations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1727,4 +1727,43 @@ test.group('Relations | HasOne', (group) => {

assert.equal(userQuery.sql, helpers.formatQuery('select * from "users" where exists (select * from "profiles" where users.id = profiles.user_id and "profiles"."deleted_at" is null)'))
})

test('fetch nested relations with same root', async (assert) => {
class Car extends Model {
}

class Profile extends Model {
}

class User extends Model {
car () {
return this.hasOne(Car)
}

profile () {
return this.hasOne(Profile)
}
}

class Identity extends Model {
user () {
return this.belongsTo(User)
}
}

[Car, Profile, User, Identity].forEach(model => {
model._bootIfNotBooted()
})

await ioc.use('Database').table('users').insert({ username: 'virk' })
await ioc.use('Database').table('profiles').insert({ user_id: 1, profile_name: 'virk', likes: 3 })
await ioc.use('Database').table('cars').insert({ user_id: 1, name: 'Peugeot', model: '307' })
await ioc.use('Database').table('identities').insert({ user_id: 1, is_active: true })

const identities = await Identity.query().with('user.car').with('user.profile').fetch()
const user = identities.first().getRelated('user')

assert.exists(user.getRelated('car'))
assert.exists(user.getRelated('profile'))
})
})

0 comments on commit c8cc658

Please sign in to comment.