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.

Filter model based on a field from one of its relations

See original GitHub issue

I must be missing something very basic, because it looks to me that this has to be a very common use case and I cannot figure out how to do it.

Let’s say I have a User and a Client models like this:

var User = bookshelf.Model.extend({
  tableName: 'users',
  client: function() {
    return this.belongsTo(Client);
  }
});

var Client = bookshelf.Model.extend({
  tableName: 'clients',
  users: function() {
    return this.hasMany(User);
  }
});

Client has a field called “is_active”. Now I want to get all users whose Client is active. Using raw knex I would do something as simple as this:

knex('users AS u') .join('clients AS c', 'c.id', 'u.client_id') .where({ 'c.is_active': true, }).then(function(users) {});

How would I do this using bookshelf? I only seem to find ways to filter the relation, but not to filter the model itself using fields from the relation.

Thanks.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
helios1138commented, Feb 17, 2016

actually it would’ve been great to have some kind of query helper for this so you could do something like this:

Model
  .query('join', Model.related('others'))
  .where('others.is_active', true)
  .fetch()

or this:

Model
  .query(q => Model.related('others').joinTo(q))
  .where('others.is_active', true)
  .fetch()

Should be pretty straitforward since all the information is already there in .relatedData and there is no need for rewriting relation fetching itself.

1reaction
rhys-vdwcommented, Feb 17, 2016

@helios1138 is correct. Currently relations do not know how to perform joins. You’ll have to manually add a join in the query callback.

Something like this:

Model
  .query('join', 'others', 'models.id', 'others.model_id')
  .where('others.is_active', true)
  .fetch()
Read more comments on GitHub >

github_iconTop Results From Across the Web

django - Filtering one model based on another model's field
You filter with: postresult = Post.objects.filter( post_user__follow_target__follow_user=request.user ). The post_user will follow the ...
Read more >
Aggregation | Django documentation
Here's how to do common aggregate queries, assuming the models above: ... Any filter() (or exclude() ) applied to normal model fields will...
Read more >
Model relationships in Power BI Desktop - Microsoft Learn
A model relationship propagates filters applied on the column of one model table to a different model table. Filters will propagate so long...
Read more >
Filter Data Across Multiple Data Sources - Tableau Help
Follow the steps below to learn how to filter data across multiple data sources. ... To edit an existing relationship, select the fields...
Read more >
Getting Started — django-filter 22.1 documentation
Declaring filters¶ · field_name : The name of the model field to filter on. You can traverse “relationship paths” using Django's __ syntax...
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