【问题标题】:sequelize Nested include with where clausesequelize 嵌套包含 where 子句
【发布时间】:2017-08-27 08:01:27
【问题描述】:

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

我当前的表结构:

  • Table1 包含项目(有图像)和用户(不相关)
  • 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 子句
  • @Adiii 这给了我一个unknown column 'Table2.Table3.Table4.Table4id' in field list 错误。
  • 你需要什么?实际上,如果您需要内部包含中的 where 子句,我没有得到您的问题,然后告诉我
  • var user = require('../models/').table1; var user = require('../models/').table2;包括:[{模型:table1,必需:true,包括:[{模型:table2,必需:true,其中:{condtion}}]}]
  • 但是根据文档,您在内部包含位置的方式是无效的

标签: mysql node.js express sequelize.js


【解决方案1】:

你只需要把where的方式,你也可以制作选项对象,然后将它传递到你需要的地方

  • 如果需要,将where condition 放入每个include
  • required true and false更改加入规则

当使用 include.where 过滤预先加载的模型时 include.required 隐式设置为 true。这意味着一个内 join 完成后返回带有任何匹配子代的父模型。

  • sequelize 称它为eager-loading 更多详情请访问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);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    • 2014-10-17
    • 2021-11-23
    相关资源
    最近更新 更多