使用包含和限制来对关键字进行搜索,导致包含表上缺少FROM子句条目

问题描述

我正在针对Post的列及其关联的表列实施常规的关键字搜索。

发布归属于类别。 发表hasMany Like。

当我通过组合where。$ or,限制/偏移量和顺序尝试此过滤器时,出现错误missing FROM-clause entry for table "category"

我认为这是因为sequelize似乎只在主查询上应用了所包含模型的外部联接,而不是在用于过滤器/限制的内部查询上应用了,但是不确定我如何解决该问题。非常感谢任何解决方案或建议!

js

const { rows: posts,count } = await Post
    .findAndCountAll({
      where: {
        $or: [ 
          { title: { $iLike: `%${searchKeyWord}%` } },{ '$category.label$': { $iLike: `%${searchKeyword}%` } },],},include: [
        { model: Category },// Like is not used in the where clause above,but this is required to reproduce the issue.
        { model: Like },limit: 25,offset: 0,order: [['id','desc'],})
   

记录的sql。注释行是我添加的,而不是续集

SELECT          "post".*,"likes"."id"           AS "likes.id","likes"."userId"       AS "likes.userId","likes"."postId"       AS "likes.postId","likes"."createdAt"    AS "likes.createdAt","likes"."updatedAt"    AS "likes.updatedAt","likes"."deletedAt"    AS "likes.deletedAt","category"."id"        AS "category.id","category"."label"     AS "category.label","category"."createdAt" AS "category.createdAt","category"."updatedAt" AS "category.updatedAt","category"."deletedAt" AS "category.deletedAt" 
FROM            ( 
                         SELECT   "post"."id","post"."title","post"."content","post"."createdAt","post"."updatedAt" 
                         FROM     "posts" AS "post" 
// adding left outer join for category here seems to solve the problem. ex)
//                       LEFT OUTER JOIN "categories" AS "category" 
//                       ON              "post"."categoryId" = "category"."id" 
                         WHERE    ( 
                                           "post"."title" ilike '%english%' 
                                  OR       "category"."label" ilike '%english%') 
                         ORDER BY "post"."id" DESC limit 25 offset 0) AS "post" 
LEFT OUTER JOIN "likes"                                               AS "likes" 
ON              "post"."id" = "likes"."postId" 
LEFT OUTER JOIN "categories" AS "category" 
ON              "post"."categoryId" = "category"."id" 
ORDER BY        "post"."id" DESC;

我正在使用 续6.3.5 nodejs 10.22.0

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...