Last active
November 5, 2019 13:49
-
-
Save shcyiza/2a87db67253c9f293295984133ede8f5 to your computer and use it in GitHub Desktop.
SSCCE paginate where statement with included model issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
module.exports = async function(createSequelizeInstance, log) { | |
const { Sequelize, Op, Model, DataTypes } = require('sequelize'); | |
const sequelize = createSequelizeInstance({ benchmark: true }); | |
await sequelize.authenticate(); | |
const Report = sequelize.define('Report', { | |
name: DataTypes.STRING, | |
description: DataTypes.TEXT | |
}); | |
const User = sequelize.define('User', { | |
email: DataTypes.STRING, | |
name: DataTypes.STRING, | |
age: DataTypes.INT, | |
}); | |
const Commment = sequelize.define('Comment', { | |
text: DataTypes.TEXT, | |
user_id: DataTypes.INT, | |
report_id: DataTypes.INT, | |
}); | |
Report.hasMany(Comment); | |
User.hasMany(Comment); | |
Comment.belongsTo(Report); | |
Comment.belongsTo(USer); | |
await sequelize.sync(); | |
const Op = sequelize.Op; | |
const all_reports_with_adult_users_comments = await Report.findAll({ | |
where: { | |
"$comments.user.age$": { [Op.gte]: 18 }, | |
}, | |
include: [{ | |
model: Comment, | |
required: false, | |
include: [User] | |
}], | |
limit: 10, | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment