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.

Querying Related Tables on a Model

See original GitHub issue

I have an Ap Model fo aps Table defined as :

var Ap = Bookshelf.Model.extend({
    tableName:'aps',
    idAttribute:'ethernet_mac',
    hasTimestamps: true,
    venue: function() {
        return this.belongsTo(Venue,'venue_name');
    },
    switch: function() {
        return this.belongsTo(Switch,'switch_id');
    },
    wlc: function(){
        return this.belongsTo(Wlc,'wlc_id','id');
    }
});

If I want to query the aps Table, i do the following:

    Ap.query(function(qb){
        qb.where('venue_name','LIKE','AUS%');
    })
    .fetchPage({
        limit:3, 
        offset:0, 
        withRelated: [
        {wlc:function(qb){qb.column('id','name','ip');}},
        'switch'
        ]})
    .then(function (resultCol){
        res.status(200).json({aps:resultCol.toJSON()});
    })
    .catch(function (err){
        console.log("Err",err);
        res.status(500).json({error:err});
    });

The above gives me 3 Ap records with all of their related switch info & only the ‘id’,‘name’,‘ip’ of their related wlc info.

How would I query the relatedTables (wlcs,switchs,venues) in BookShelf to get matching Ap records?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
ricardogracacommented, May 14, 2018

I don’t see how dropping down to knex.raw() is an acceptable solution…

Because it’s the only way right now. I agree it sucks, but no one has implemented what you want yet.

Why can’t bookshelf let me do something as simple as…

Because if it was that simple to implement someone would have done it already, so clearly it’s not simple. This has been an issue for a long time and many already reported it, but no one actually did the required work. This will require a very extensive refactor of Bookshelf, which will happen at some point, but for now knex.raw will have to do. The issue here is that these relations are not fetched using JOIN, but using two separate queries. See issue #141 for more info.

For the record, this is a duplicate of #141.

2reactions
VanessaRLewiscommented, Apr 21, 2017

Solved this using knex.raw()

Read more comments on GitHub >

github_iconTop Results From Across the Web

Relationships between tables in a Data Model
A Data Model can have multiple relationships between two tables. To build accurate calculations, Excel needs a single path from one table to...
Read more >
Creating multiple tables and table relationships - Launch School
Having all our data in one table can make for very difficult data management. We can split data in specialized tables that are...
Read more >
Power Query Merge? Data Model Relationships? - YouTube
Excel Business Analytics #39: Import Related Tables : Power Query Merge? Data Model Relationships?
Read more >
SQL JOIN TABLES: Working with Queries in SQL Server
In a relational database, multiple tables are connected to each other via foreign key constraints. If you want to retrieve data from related...
Read more >
Defining table joins for a query subject - IBM
You can join logically related tables in a query subject so that the model properly represents the logical structure of your business.
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