使用 bookshelf/knex 时缺少左连接

Missing left join when using bookshelf/knex

出于某种原因,在某些情况下,knex 不会将左连接添加到查询中。我试图用最少的代码重现该错误,所以我尝试将 knex 与内存中的 sqlite3 一起使用。

var knex = require('knex')({
    client:'sqlite3',
    connection: {
    filename: ":memory:"
    },
    debug: true
});

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

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

Conversation
    .where(qb => {
        qb
            .leftJoin('conversations_recipients', function () {
                this.on('conversations_recipients.conversationId', 'conversations.id');
            });
    })
    .fetch();

当我检查控制台上的调试消息时,我得到:

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

并且缺少左连接。有人知道这段代码有什么问题,我怎样才能包含所需的连接?

提前致谢。

您正在呼叫 where 而不是 query。此外,除非您正在执行复杂的连接逻辑,否则您不需要使用 join 回调。请参阅 where and join. And Bookshelf documentation for query

的 knex 文档
Conversation.query(qb =>
  qb.leftJoin(
    'conversations_recipients',
    'conversations_recipients.conversationId',
    'conversations.id'
  );
).fetch().then(conversations => { // ...