sequelize嵌套包含where子句

时间:2017-04-02 11:45:25

标签: mysql node.js express sequelize.js

我使用sequelize进行过滤。

我目前的表结构:

  • 表1包含项目(包含图像)和用户(不相关)
  • 表1与表1中的表2至表2中的直接关系(不是表3)
  • 表2与表2中的表3至表3中的直接关系(不是表4)
  • 表3与表3中的表4至表4具有直接关系

我想过滤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,但无济于事。

如何过滤嵌套包含?有没有其他方法可以构建查询,所以我不必拥有嵌套包含,但仍然保留这个数据结构(有没有比我想到的更好的结构方式)?< / p>

编辑:所以我希望能够对包含的表进行排序,并在顶级where子句中至少设置一个参数(如deleted = 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在字段列表中是未知的。

1 个答案:

答案 0 :(得分:7)

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

    如果需要,
  • where condition放入include
  • required true and false更改joing规则
  

当使用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) })