sequelize where date clause

帅比萌擦擦* 提交于 2021-02-08 09:13:07

问题


  • OS: Linux (Lubuntu)
  • language: Javascript (Node js)
  • framework: express js
  • Database: mysql
  • "data" is a Date field from "activitat" table

When I run the next statement using Sequelize.js

models.TblActivitat.findAll(
                {
                    attributes: 
                    [
                        'codiAct', 'procedencia', 'pacient', 'proces', 'prova', 'rmn', 'realitzador',
                        [Sequelize.fn('date_format', Sequelize.col('data'), '%d-%m-%Y'), 'data']
                    ],
                    include: models.TblTecnics,
                    where: {"data": '2016-10-20' },   //dataAAAAMMDD
                    order: "registre DESC"
                }).then(function(tblActTec){
                    ...
                });

... I'd have to get some records, but I get nothing

running on mysql I get the results:
mysql> select data from activitat where data = '2016-10-20';
+------------+
| data |
+------------+
| 2016-10-20 |
| 2016-10-20 |
| 2016-10-20 |
| 2016-10-20 |
+------------+

If I see the console of the Node server. When the satement before is executed, it shows as follow:

Executing (default): SELECT activitat.codiAct activitat.procedencia, activitat.pacient, activitat.proces, activitat.prova, activitat.rmn, activitat.realitzador, date_format(data, '%d-%m-%Y') AS data, tecnic.codiTec AS tecnic.codiTec, tecnic.nom AS tecnic.nom FROM activitat AS activitat LEFT OUTER JOIN tecnics AS tecnic ON activitat.realitzador = tecnic.codiTec WHERE activitat.data = '2016-10-19 22:00:00'
ORDER BY registre DESC;

my question is:
I wrote "2016-10-20" as the where clause. When I do it in mysql it shows the results I hope, but when sequelize is executed it changes the value of data clause by "2016-10-19 22:00:00" (two hours before "2016-10-20 00:00:00"!!). Why?

I remeber you that "data" field is a Date field (not a DateTime field)

Thank you very very much!!


回答1:


Sequelize converts the string you pass in to a Date object, which has a time assigned to it. If you want to select records on a certain date (rather than at an exact time) you can do so like this:

 date: {
     $lt: new Date('2016-10-20'),
     $gt: new Date(new Date('2016-10-20') - 24 * 60 * 60 * 1000)
 }

Or in your case:

models.TblActivitat.findAll(
            {
                attributes: 
                [
                    'codiAct', 'procedencia', 'pacient', 'proces', 'prova', 'rmn', 'realitzador',
                    [Sequelize.fn('date_format', Sequelize.col('data'), '%d-%m-%Y'), 'data']
                ],
                include: models.TblTecnics,
                where: {"data": {
                    $lt: new Date('2016-10-20'),
                    $gt: new Date(new Date('2016-10-20') - 24 * 60 * 60 * 1000)
                } },   //dataAAAAMMDD
                order: "registre DESC"
            }).then(function(tblActTec){
                ...
            });



回答2:


I've discovered a new field type when you define the fields of the table in Sequelize. This is the DATAONLY type (instead of DATA type). So, the field definition follows as:

        data:
        {
            type:   DataTypes.DATEONLY,
            validate:
            {
                notEmpty: 
                {
                    msg: "-> Falta data"
                }
            }
        },

With this file type (DATAONLY) it only considers de date part (not the time part)



来源:https://stackoverflow.com/questions/39926831/sequelize-where-date-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!