小编C. *_*lik的帖子

使用 where 内包含的 Sequelize 查询

我有以下型号:

'use strict';

module.exports = function(sequelize, DataTypes) {
    var Collection = sequelize.define("Collection", {
        name: DataTypes.STRING,
        customer: DataTypes.STRING
    }, {
        classMethods: {
            associate: function(models) {
                Collection.hasMany(models.Items);
            }
        }
    });

    return Collection;
};


'use strict';

module.exports = function(sequelize, DataTypes) {
    var Item = sequelize.define("Item", {
        itemId: {
            type: DataTypes.STRING,
            primaryKey: true
        }
    }, {
        classMethods: {
            associate: function(models) {
                Item.belongsToMany(models.Collection);
            }
        }
    });

    return Item;
};
Run Code Online (Sandbox Code Playgroud)

假设我想获取特定客户的所有集合及其项目,其中一个项目包含 itemId。我的查询如下:

models.Collection.findAll({
    where: {
      customer: customerParam
    },
    include: [{
        model: models.Item,
        where: {
          itemId: itemParam …
Run Code Online (Sandbox Code Playgroud)

node.js express sequelize.js

7
推荐指数
1
解决办法
8445
查看次数

标签 统计

express ×1

node.js ×1

sequelize.js ×1