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.

Performing a collection query, with where conditions, and including relations

See original GitHub issue

Hey there,

I have two tables, activity_group and roundtable_user. activity_group joins to roundtable_user on activity_group.performer_id = roundtable_user.id and the models/collections look like:

var ActivityGroup = bookshelf.Model.extend({
  tableName: 'activity_group',

  performer: function () {
    return this.belongsTo(RoundtableUser)
  }
})

var RoundtableUser = bookshelf.Model.extend({
  tableName: 'roundtable_user'
}

var ActivityGroupCollection = bookshelf.Collection.extend({
  model: ActivityGroup
})

I want to basically do a query from activity_group where (some filters here) and then including the performer relation. In other words, here’s what I want to say:

ActivityGroupCollection
  .fetch({withRelated: ['performer']})
  .where({performer_id: ..., ... })

This way, each ActivityGroup has performer() filled in. Obviously, though, I can’t write this. I looked through the code and the docs but I couldn’t figure out how to do this. Can you help?

Issue Analytics

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

github_iconTop GitHub Comments

9reactions
sgentilecommented, May 8, 2015

What if you wanted the ‘withRelated’ to not be a separate query executed, but instead be a join ?

ie. get the activity group with the performer where the performer name is ‘steve’ ?

4reactions
ishaadXcommented, Nov 14, 2016

Hello,

how can I write down a bookshelf query same as mysql’s “between … and” query

thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create a query based on multiple tables
Build a select query by using tables with a many-to-many relationship · On the Create tab, in the Queries group, click Query Design....
Read more >
Perform simple and compound queries in Cloud Firestore
Cloud Firestore provides powerful query functionality for specifying which documents you want to retrieve from a collection or collection group.
Read more >
Relation queries (Concepts)
Prisma Client provides convenient queries for working with relations, such as a fluent API, nested writes (transactions), nested reads and relation filters.
Read more >
Query Documents — MongoDB Manual
MongoDB Manual. How do I query documents, query top level fields, perform equality match, query with query operators, specify compound query conditions.
Read more >
MongoDB Join Two Collections Simplified
We can join documents on collections in MongoDB by using the $lookup (Aggregation) function.
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