简体   繁体   English

使用书架/ knex时缺少左连接

[英]Missing left join when using bookshelf/knex

for some reason, in some cases knex doesn't add left join to the query. 由于某些原因,在某些情况下knex不会向查询添加左联接。 I tried to reproduce the bug with minimal code, So I tried to use knex with in-memory sqlite3. 我试图用最少的代码重现该错误,因此我尝试将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();

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. 提前致谢。

You're calling where instead of query . 您在调用where而不是query Also you don't need to use the join callback unless you're doing complex join logic. 另外,除非您要执行复杂的联接逻辑,否则无需使用join回调。 Refer to knex documentation for where and join . 有关where join信息,请参阅knex文档。 And Bookshelf documentation for query 和书架文档进行query

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM