20

Tables have many to many relationship, junction by an order table in between.

Outlet --> Order <-- Product

I want to get the list of Outlet for today Order.

So here is a function to get all outlets:

db.Outlet.findAll({include: [
    {model:db.Product, attributes: ['id', 'name', 'nameKh']}
    ]}).then(function(outlets){
    return res.jsonp(outlets);
})

I got this result:

enter image description here

I can only select with where Product Id by using this:

db.Outlet.findAll({include: [
    {model:db.Product, attributes: ['id', 'name', 'nameKh'], where: {id: 2}
    ]}).then(function(outlets){
    return res.jsonp(outlets);
})

How can I query by specific order amount, or today order date?

Here are my models:

Outlet:

var Outlet = sequelize.define('Outlet', {
            outletCode: DataTypes.STRING,
            outletName: DataTypes.STRING,
            outletNameKh: DataTypes.STRING,
            outletSubtype: DataTypes.STRING,
            perfectStoreType: DataTypes.STRING,
            address: DataTypes.STRING
        },
        {
            associate: function(models){
                Outlet.belongsToMany(models.Product, {through: models.Order});
                Outlet.belongsTo(models.Distributor);
                // Outlet.hasMany(models.Order);
            }
        }
    );

Product:

var Product = sequelize.define('Product', {
            inventoryCode: DataTypes.STRING,
            name: DataTypes.STRING,
            nameKh: DataTypes.STRING,
            monthlyCaseTarget: DataTypes.INTEGER,
            pieces: DataTypes.INTEGER,
            star: DataTypes.BOOLEAN,
            price: DataTypes.FLOAT,
            active: DataTypes.BOOLEAN
        },
        {
            associate: function(models){
                Product.belongsToMany(models.Outlet, {through: models.Order});
                Product.belongsTo(models.Category);
                // Product.hasMany(models.Order);
            }
        }
    );

Order:

var Order = sequelize.define('Order', {
            id: {
                type: DataTypes.INTEGER,
                primaryKey: true,
                autoIncrement: true
            },
            amount: DataTypes.INTEGER
        },
        {
            associate: function(models){
                Order.belongsTo(models.Outlet);
                Order.belongsTo(models.Product);
                Order.belongsTo(models.User);
            }
        }
    );
1
  • 7
    Thanks for this clear question with examples, I even learned from it!
    – antew
    Feb 21, 2017 at 18:44

2 Answers 2

23

Try it:

db.Outlet.findAll({
    include: [{
        model:db.Product, 
        attributes: ['id', 'name', 'nameKh'], 
        through: { where: { amount: 10 } }
    }]
})
5
  • possible to put where out of include?
    – Ron
    Sep 16, 2017 at 0:14
  • 4
    yes, @Ron. you should wrap where statement like it ..findAll({include:[...], where: {'$product.amout$': 10} }) Sep 18, 2017 at 4:34
  • 2
    @TilekbekovYrysbek you just saved my life with this last comment. THANK YOU Apr 16, 2020 at 21:38
  • 1
    what if i want to search multiple amount?
    – Alan Yong
    Jun 13, 2021 at 8:55
  • God bless you, bro.
    – Eidris
    Jul 24, 2022 at 22:41
2

Similar case

Super many to many relatioship

User.belongsToMany(Profile, { through: Grant });
Profile.belongsToMany(User, { through: Grant });
User.hasMany(Grant);
Grant.belongsTo(User);
Profile.hasMany(Grant);
Grant.belongsTo(Profile);

This way, we can do all kinds of eager loading:

// All these work:
User.findAll({ include: Profile });
Profile.findAll({ include: User });
User.findAll({ include: Grant });
Profile.findAll({ include: Grant });
Grant.findAll({ include: User });
Grant.findAll({ include: Profile });

We can even perform all kinds of deeply nested includes:

User.findAll({
  include: [
    {
      model: Grant,
      include: [User, Profile]
    },
    {
      model: Profile,
      include: {
        model: User,
        include: {
          model: Grant,
          include: [User, Profile]
        }
      }
    }
  ]
});

From sequelize doc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.