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.

specify columns to load with relations

See original GitHub issue

Is it possible?

new User({id: 2}).fetch({
  withRelated: ['account'],
  columns: ['id', 'name', 'accountId']
})

I want to load ['id', 'name', 'accountId'] columns from users table and load ['balance', 'total'] from accounts table.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:22 (7 by maintainers)

github_iconTop GitHub Comments

12reactions
tgriessercommented, Sep 28, 2013

Yes - I had an undocumented method in knex called column which is now documented and working correctly in the just released 0.4.7… I believe that:

new User({id: 2}).fetch({
  withRelated: [{'account': function(qb) {
    qb.column('id', 'balance', 'total');
  }}],
  columns: ['id', 'name', 'accountId']
})

should take care of it (assuming id is the idAttribute of the account table. Give it a shot with the latest Knex and let me know if that works.

2reactions
roackb2commented, Dec 2, 2016

Hi there, I’m also facing the exact same issue as @howardmann have, I can’t use the select clause on an hasMany relationships, any updates here?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Laravel Eager Loading - Load only specific columns
I've just updated the original answer, check it now. Basically, if Owner -> Car relationship is set on owners.id -> cars.owner_id , you...
Read more >
Eloquent eager loading specific columns - Laracasts
Eloquent eager loading specific columns. I have a Ticket model which and a belongsTo relationship for ticket subject. Copy Code Ticket::with('subject')->get ...
Read more >
Eager loading specific columns in a nested relationship
For eager loading a nested relationship you always have to include the primary key or the foreign key to the field list. Wrong....
Read more >
Laravel Eager Loading with Selected Columns Example
In this post, we will lean how to select specific columns with eager loading relation in laravel. we can get specific columns using...
Read more >
How to Eager Loading Specific Columns in Laravel - PostSrc
To eager load only specific column you can specify the relationship then add the column you want it to load after specifying a...
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