How to make query with order by with current date

I want to join 2 tables and want to show latest post first based on current date for example today’s publish post will show at top then future date then past date’s post will show. I need to write this query in sequelize. I am getting unknown column error ‘postModel.DATE(published_at)’ My sequelize query is like that –

postModel.findAndCountAll({                include:[                 { model:userModel,                    where: { user_id:  user_id},                   required:false                   },                 ],                 order: [                    [ 'DATE(published_at) = DATE(NOW())', 'DESC']                 ],                 limit: limit,                 offset: offset,                                }); 

Following raw query is working well to me

SELECT * FROM posts as P JOIN user as U ON U.id = P.user_id where ORDER BY     DATE(P.published_at)=DATE(NOW()) DESC,     DATE(P.published_at)<DATE(NOW()) DESC,     DATE(P.published_at)>DATE(NOW()) ASC` 
Add Comment
1 Answer(s)

remove your date function and query like this .

postModel.findAndCountAll({   include: [{ model: userModel, where: { user_id: user_id },required:false }],   where: {     published_at: {       [Op.eq]: Date(NOW()),     },   },   order: [["published_at", "DESC"]],   limit: limit,   offset: offset, }); 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.