Error in Sequelize SQL query

1

I have this query:

        where: {
            $or: {
                $and: {
                    mes: mes,
                    any: any
                },
                $and: {
                    dia: 1,
                    mes: 1 + Number(mes),
                    any: any
                }
            }
        }

I try to select the records of a month and year, plus the first day of the following month. Everything seems correct. There is no compilation error, but in execution it does give an error like syntax: syntax error near ",".

Would anyone know why?

Update

The query I want to pass to Sequelize is the following:

WHERE ('Partes'.'mes'=3 AND 'Partes'.'anio'=2017 
      OR 'Partes'.'dia'=1 AND 'Partes'.'mes'=4 AND 'Partes'.'anio'=2017)
      ORDER BY 'fecha' ASC, 'Comments'.'codigo' ASC;

Thank you.

    
asked by Jota 10.03.2017 в 18:36
source

3 answers

2

I see that you are still in this, try this way.

where: Sequelize.or(
    Sequelize.and(
        {mes: mes},
        {anio: anio}
    ),
    Sequelize.and(
        {dia: 1},
        {mes: 1 + Number(mes)},
        {anio: anio}
    )
)
    
answered by 11.03.2017 / 17:53
source
2

You can try it in the following way:

    where: {
        $or: {
            $and: {
                mes: mes,
                any: any
            },
            $and: {
                dia: 1,
                mes: (1 + Number(mes)),
                any: any
            }
        }
    }
    
answered by 10.03.2017 в 18:47
1
  

I try to select the records of a month and year, plus the first day of the following month

So, why do you use OR if you just have AND ? In addition, helper $or of Sequelize accepts an array, not a literal object. Your query is simplified like this:

where: {
  mes, anio,
  $and: {
    anio,
    dia: 1,
    mes: mes + 1
  }
}

I assume that mes is an integer so I add it as is. In case it is a string (rare because in dia you pass an integer), then leave the Number(mes) .

Update

The query that you have put in the update of your question translates to:

Model.findAll({
  where: {
    $or: [
      {
        $and: {
          mes: 3,
          anio: 2017
        }
      },
      {
        $and: {
          dia: 1,
          mes: 4,
          anio: 2017
        }
      }
    ],
    order: 'fecha codigo ASC'
  }
})
.then(models => {

});

The first $and is translated to:

mes = 3 AND anio = 2017

The second $and is translated to:

dia = 1 AND mes = 4 AND anio = 2017

And the $or that surrounds them is interpreted as:

WHERE
  mes = 3 AND anio = 2017 OR
  dia = 1 AND mes = 4 AND anio = 2017
    
answered by 10.03.2017 в 19:33