Querying Related Tables on a Model
See original GitHub issueI 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:
- Created 6 years ago
- Comments:5 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Because it’s the only way right now. I agree it sucks, but no one has implemented what you want yet.
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 usingJOIN
, but using two separate queries. See issue #141 for more info.For the record, this is a duplicate of #141.
Solved this using knex.raw()