someone235 someone235 - 1 year ago 108
Node.js Question

Missing left join when using bookshelf/knex

for some reason, in some cases knex doesn't add left join to the query. I tried to reproduce the bug with minimal code, So I tried to use knex with in-memory sqlite3.

var knex = require('knex')({
connection: {
filename: ":memory:"
debug: true

var bookshelf = require('bookshelf')(knex);

var Conversation = bookshelf.Model.extend({
tableName: 'conversations',

.where(qb => {
.leftJoin('conversations_recipients', function () {
this.on('conversations_recipients.conversationId', '');

When I check the debug messages on the console, I get:

{ method: 'select',
options: {},
bindings: [ 1 ],
sql: 'select "conversations".* from "conversations" limit ?' }

And there's the left join is missing. Someone knows what's wrong with this code, and how can I include the desired join?

Thanks in advance.

Answer Source

You're calling where instead of query. Also you don't need to use the join callback unless you're doing complex join logic. Refer to knex documentation for where and join. And Bookshelf documentation for query

Conversation.query(qb =>
).fetch().then(conversations => { // ...