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

某人235

由于某些原因,在某些情况下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和的信息,请参阅knex文档join和书架文档query

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章