'PostgreSQL double colon to Sequelize query

select column_name::date, count(*) from table_name group by column_name::date

What is the equivalent of this SQL query in Sequelize? I couldn't find what to do when there is "double colon" in PostgreSQL query.



Solution 1:[1]

Thanks to a_horse_with_no_name comment I decide to use;

sequelize.literal("cast(time_column_name as date)")

with the grouping section and the latest code take form;

ModelName.findAndCountAll({
  attributes: [
    [sequelize.literal("cast(time_column_name as date)"), "time_column_name"],
  ],
  group: sequelize.literal("cast(time_column_name as date)"),
})

So, it gives two SQL query (because of findAndCountAll() function);

SELECT count(*) AS "count"
FROM "table_name"
GROUP BY cast(time_column_name as date);

AND

SELECT cast(time_column_name as date) AS "time_column_name"
FROM "table_name"
GROUP BY cast(time_column_name as date);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 yalin