首页 > 使用sequelize定义自身关联,如何为关联查询添加查询条件?

使用sequelize定义自身关联,如何为关联查询添加查询条件?

使用__Sequelize__定义了Category,然后category可能有二级category,所以定义了category跟自己本身的hasmany的关系:

Category.hasMany(Category, {foreignKey: 'pid', through: null });

然后在category的findAll方法中添加了用于联合查询的include属性

Category.findAll({
    ...
    include: {
        model: Category,
        attributes: [...],
        where: {
            is_deleted: 0
        }
    }
})

在上面的where条件中我加上is_deleted是想把满足is_deleted=0的二级category查询出来,但是加上这个where以后结果集为空,去掉就可以(当然这时候不满足我的要求,is_deleted=1的类目也被查询出来了)

请问应该如何解决?

【热门文章】
【热门文章】