Sequelize mapping does not work using max and col

Viewed 353

I am using sequelize 5.21.8 and I have to calculate the min and max of a column, so I use fn.min and fn.max .

My mapping is like so

startTime: {
  field:'signaldate',
  type: Sequelize.DATE
} ,

and for that column my query is

    return Signals.findAll({
      attributes:[
        [Sequelize.fn('max', Sequelize.col('signaldate')), 'maxdateallowed'],
        [Sequelize.fn('min', Sequelize.col('signaldate')), 'maxdateallowed']
      ]   
    })

The problem is that I have to use the original column name inside the Sequelize.fn('max', in the Sequelize.col part. The query works, but the mapping does snot.

If I use the startTime as I set in the mapping, like so

    [Sequelize.fn('max', Sequelize.col('startTime')), 'maxdateallowed'],

then I get an error

SequelizeDatabaseError: Unknown column 'startTime' in 'field list'

How can I use mapping in that case and not have an error?

Thanks

2 Answers

The mapping does not work because you are mapping the max and min to the same column maxdateallowed. For the min map it to something like mindateallowed (mindateallowed)

return Signals.findAll({
    attributes: [
        [Sequelize.fn('max', Sequelize.col('signaldate')), 'maxdateallowed'],
        [Sequelize.fn('min', Sequelize.col('signaldate')), 'mindateallowed']
    ]
})
Related