mysql - How to order by an aggregate function in sequelize -
i feel if i've hit brick wall while converting old php app functionality on node , sequelize. maybe i'm approaching limits of orm can do, want ask before give up.
let's imagine 3 tables: video
, task
, publishdate
.
the application requires video
may have multiple publishdate
s , multiple task
s. task
s have due dates calculated looking @ minimum of video
's publishdate
s.
so if video
has 2 publishdate
s of 2015-12-01
, 2015-08-15
, we'd use 2015-08-15
, perform additional calculations on task
due date.
the problem occurs when i'm trying list of task
s ordered calculated due dates.
i've attempted many different approaches. latest try attempts use sequelize.fn
function
models.task.findall({ where: {isactive: false, iscompleted: false}, include: [ { model: models.video, attributes: [[sequelize.fn('min', 'video.publishdates.date'), 'duedate']], include: [models.publishdate] } ], order: [['video', 'duedate', 'asc']], limit: 50 })
i'm not encouraged i'm headed in right direction because without order clause, attempt returns 1 record. still, feels closest i've been day , i'm missing bit of information or syntax make possible.
this first foray world of orms, apologize if there simple i'm missing. tried best looking documentation , other questions on google no luck far.
you need order using function. should try:
order : [['video','asc'],[sequelize.fn('min', 'video.publishdates.date'), 'asc]]
Comments
Post a Comment