小编pir*_*ire的帖子

是否可以使用sequelize通过关联表中的属性过滤查询?

我试图通过连接表的属性过滤我的查询

我有两个表格城市和类别,我通过第三个表CityCategory关联.我们的想法是在与城市相关联时获取类别CityCategory.year是一个特定的整数.

这是我指定关联的方式:

module.exports = function(sequelize, DataTypes) {
    var CityCategory = sequelize.define('CityCategory', {
        year: {
            type: DataTypes.INTEGER,
            allowNull: false,
            validate: {
                notNull: true
            }
        }
    }, {
        indexes: [{
            unique: true,
            fields: ['CityId', 'CategoryId', 'year']
        }]
    });

    return CityCategory;
};

City.belongsToMany(models.Category, {
                    through: {
                        model: models.CityCategory
                    }
                });

Category.belongsToMany(models.City, {
                    through: {
                        model: models.CityCategory
                    }
                });
Run Code Online (Sandbox Code Playgroud)

这是我目前的查询,但未成功使用:

City.find({
        where: {id: req.params.id},
        attributes: ['id', 'name'],
        include: [{
            model: Category,
            where: {year: 2015},
            attributes: ['id', 'name', 'year']
        }] …
Run Code Online (Sandbox Code Playgroud)

node.js sequelize.js

18
推荐指数
2
解决办法
8605
查看次数

标签 统计

node.js ×1

sequelize.js ×1