繁体   English   中英

sequelize 嵌套包含 where 子句

[英]sequelize Nested include with where clause

我正在使用 sequelize 进行一些过滤。

我当前的表结构:

  • 表 1 包含项目(包含图像)和用户(不相关)
  • Table1 通过 Table1 上的 Table2id 与 Table2 有直接关系(而不是与 Table3)
  • Table2 通过 Table2 上的 Table3id 与 Table3 有直接关系(而不是与 Table4)
  • Table3 通过 Table3 上的 Table4id 与 Table4 有直接关系

我也想过滤 Table3 和 Table4,因为我只能使用顶级 where 子句过滤 Table2。

我填写 where 条件的方式只是使用基础对象:

var Table2id       = parseInt(req.query.Table2id) || null,
    Table3id       = parseInt(req.query.Table3id) || null,
    Table4id       = parseInt(req.query.Table4id) || null,
    whereCondition = { deleted: 0 }

if (Table2id) { whereCondition['table2id'] = Table2id }
if (Table3id) { whereCondition['table3id'] = Table3id }
if (Table4id) { whereCondition['table4id'] = Table4id }

Table1.findAndCountAll({
        limit: limit,
        offset: offset,
        order: 'created_at DESC',
        where: whereCondition,
        include: [
            {
                model: User,
            }, {
                model: Item,
                include: [
                    {
                        model: Image
                    }
                ]
            }, {
                model: Table2,
                include: [
                    {
                        model: Table3,
                        include: [
                            {
                                model: Table4,
                            }
                        ]
                    }
                ]
            }
        ],
}).then(function (results) { res.json(results) })

我尝试使用我发现的一些技巧,例如whereCondition['$Table3.table3id$'] = Table3id但无济于事。

如何过滤嵌套包含? 有没有另一种方法可以构造查询,这样我就不必嵌套包含,但仍保留此数据结构(是否有比我想到的更好的构造方法)?

编辑:所以我希望既能够对包含的表进行排序,又希望在顶级 where 子句中设置至少一个参数(例如已删除 = 0)。

我试过修改查询如下:

var Table2id       = parseInt(req.query.Table2id) || null,
    Table3id       = parseInt(req.query.Table3id) || null,
    Table4id       = parseInt(req.query.Table4id) || null,
    whereCondition = { deleted: 0 },
    extraWhereCondition = {}

if (Table2id) { whereCondition['table2id'] = Table2id } // figured this can be left alone in this particular case (as it works in top-level where clause)
if (Table3id) { extraWhereCondition['table3id'] = Table3id }
if (Table4id) { extraWhereCondition['table4id'] = Table4id }

Table1.findAndCountAll({
        limit: limit,
        offset: offset,
        order: 'created_at DESC',
        where: whereCondition,
        include: [
            {
                model: User,
            }, {
                model: Item,
                include: [
                    {
                        model: Image
                    }
                ]
            }, {
                model: Table2,
                include: [
                    {
                        model: Table3,
                        where: extraWhereCondition,
                        include: [
                            {
                                model: Table4,
                                where: extraWhereCondition,
                            }
                        ]
                    }
                ]
            }
        ],
}).then(function (results) { res.json(results) })

但这给了我一个错误,即 Table2.Table3.Table4.table4id 在字段列表中是未知的。

您只需要放置where的方式,您也可以制作选项对象,然后将其传递到您需要的地方

  • 如果需要,将where condition放在每个include
  • required true and false改变连接规则

当使用 include.where 过滤预先加载的模型时,include.required 被隐式设置为 true。 这意味着完成内部连接,返回具有任何匹配子项的父模型。

  • sequelize 将其称为“ eager-loading访问”以获取更多详细信息“ 急切加载”
var Table2 = require("../models/").table2; //and other model that u need

var option = {
  limit: limit,
  offset: offset,
  order: "created_at DESC",
  where: { deleted: 0 },
  include: [
    {
      model: User,
    },
    {
      model: Item,
      required: true,

      include: [
        {
          model: Image,
        },
      ],
    },
    {
      model: Table2,
      include: [
        {
          model: Table3,
          where: { deleted: 0 },
          include: [
            {
              model: Table4,
              where: { deleted: 0 },
            },
          ],
        },
      ],
    },
  ],
};

Table1.findAndCountAll(option).then(function (results) {
  res.json(results);
});

暂无
暂无

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

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