We want to include model if parent model has certain column, so is there any way to achieve this ?
Notification.findAll({
include: [{
model: this.StudentTaskModel,
attributes: ['title'],
as: 'student_task',
required: false,
where: {
'$Notifications.object_type$': 'student task'
}
}, {
model: this.UserRequestModel,
attributes: ['type', 'comment'],
as: 'user_request',
required: false,
where: {
'$Notifications.object_type$': 'user request'
}
}],
order: [
[orderBy, orderType]
],
where: {
user_id: user.id
}
})
But this is throwing error
SequelizeDatabaseError: Unknown column 'Notifications.object_type' in 'on clause'
But there is column "object_type" in parent model
So, is there any mistake with this code ?
My association is like this
Notification.associate = function (models) {
Notification.belongsTo(models.User, {
foreignKey: 'user_id',
sourceKey: 'id',
as: 'user'
})
Notification.belongsTo(models.StudentTask, {
foreignKey: 'object_id',
sourceKey: 'id',
as: 'student_task'
})
Notification.belongsTo(models.UserRequest, {
foreignKey: 'object_id',
sourceKey: 'id',
as: 'user_request'
})
};