Javascript 带连接的条件查询链

Javascript 带连接的条件查询链,javascript,knex.js,Javascript,Knex.js,尝试使用knex运行以下查询: builder.from(PageModel.tableName).where((builder) => { if (this.filters.accountId) { instance .innerJoin( PageAccountModel.tableName, `${PageModel.tableName}.id`, `${PageA

尝试使用
knex
运行以下查询:

builder.from(PageModel.tableName).where((builder) => {
      if (this.filters.accountId) {
        instance
          .innerJoin(
            PageAccountModel.tableName,
            `${PageModel.tableName}.id`,
            `${PageAccountModel.tableName}.pageId`,
          )
          .innerJoin(
            AccountModel.tableName,
            `${PageAccountModel.tableName}.accountId`,
            `${AccountModel.tableName}.id`,
          )
          .where(`${AccountModel.tableName}.id`, this.filters.accountId);
      }

      if (this.filters.type) {
        builder.where('type', this.filters.type);
      }
    });
不断获取错误:

error: missing FROM-clause entry for table "account"
at Connection.parseE (/home/jakov/Projects/platform/node_modules/pg/lib/connection.js:604:11)
at Connection.parseMessage (/home/jakov/Projects/platform/node_modules/pg/lib/connection.js:401:19)
at Socket.<anonymous> (/home/jakov/Projects/platform/node_modules/pg/lib/connection.js:121:22)
at Socket.emit (events.js:210:5)
at addChunk (_stream_readable.js:326:12)
at readableAddChunk (_stream_readable.js:301:11)
at Socket.Readable.push (_stream_readable.js:235:10)
at TCP.onStreamRead (internal/stream_base_commons.js:182:23)
错误:表“account”的子句条目中缺少
在Connection.parseE(/home/jakov/Projects/platform/node_modules/pg/lib/Connection.js:604:11)
在Connection.parseMessage(/home/jakov/Projects/platform/node_modules/pg/lib/Connection.js:401:19)
在插座上。(/home/jakov/Projects/platform/node_modules/pg/lib/connection.js:121:22)
位于Socket.emit(events.js:210:5)
在addChunk(_stream_readable.js:326:12)
在readableAddChunk(_stream_readable.js:301:11)
在Socket.Readable.push(_stream_Readable.js:235:10)
在TCP.onStreamRead(internal/stream_base_commons.js:182:23)
注意:这是从一个对象中运行的,该对象的字段上保存有过滤器(注意
This.filters


编辑:我只想抓取
页面
表。

问题是从(PageModel.tableName)之后的位置。 您正在构建类似于:

select x from foo where foo.z join bar.y
要以正确的方式构建查询联接,请执行以下操作:

select * from users join account where (users.account=account.id)
与Knwex是

knex('users').join('account').where((builder) => builder.where('user.account','account.id'))
您可以在这里测试您的查询


签出它,然后

如果您想用条件部分编写查询,您可以这样做:


const query = knex(PageModel.tableName);

if (this.filters.accountId) {
  query
    .innerJoin(
      PageAccountModel.tableName,
      `${PageModel.tableName}.id`,
      `${PageAccountModel.tableName}.pageId`,
    )
    .innerJoin(
      AccountModel.tableName,
      `${PageAccountModel.tableName}.accountId`,
      `${AccountModel.tableName}.id`,
    )
    .where(`${AccountModel.tableName}.id`, this.filters.accountId);
}

if (this.filters.type) {
  query.where('type', this.filters.type);
}

// check the query output by
console.log(query.toSQL().sql, query.toSQL().bindings);
这是可能的,因为查询生成器是可变的和可添加的,所以您也可以在初始化之后向其中添加更多子句


我很确定上面的代码就是您想要编写的代码,但我不知道它是否会为您带来正确的结果:)

很抱歉,我不得不投了反对票。连接,实际上不是那样构建的(至少使用knex),并且您正在将
account.id
作为字符串值文本而不是列引用传递。我正在使用它,似乎有问题。我从
页面
表中获得了所需的行,但是当我按
account.id
进行筛选时,
id
列的值错误。它实际上是
account.id
的值。我知道标识符语法,但我仍然希望从
页面
表中获取所有内容,而不必指定其中的每一列。另外,在
查询
对象中没有
toSql
方法……该方法是
.toSql()
而不是
.toSql()
。在SQL中连接多个表时,列名可能会发生冲突,而使它们不发生冲突的通常方法是单独列出返回的列,这些列的名称在多个表中会重叠。Knex没有任何信息,例如它如何能够自动用表名为列名加前缀。您可能希望使用
objective.js
或其他更高级别的库,例如,能够将关系作为嵌套对象来获取。我已设法将代码的第一行替换为
const query=knex(PageModel.tableName)。选择(${PageModel.tableName}.*'),因此如果您只是编辑它,我将接受这个答案作为正确的答案。谢谢你的帮助!六羟甲基三聚氰胺六甲醚。。。也许现在我想起来我的问题还不够清楚,但我只想要
页面
表格。由于过滤,连接是必需的。