Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize: how to find a Max element

I am issue while forming a sequelize query :

My Native query is something.

       SELECT max(id),vehicleID FROM `vehicles` WHERE `vehicles`.`vsr_id`=342;

For this i am trying to build Code which something like this.

    Vehicle.find({ where: { 'vsr_id': 342 }, order : [sequelize.fn('max', sequelize.col('id'))] }).success(function(vehicles){
       console.log("Something i got" + vehicles)
        })

It throwing an error : sequelize Object [object Object] has no method 'col' and i refered from this URL: http://sequelizejs.com/docs/latest/models#block-34-line-8

Any code missings from my end. Please help me out

FYI: Ignore semicolons

like image 839
Ayyappa A Avatar asked May 07 '14 06:05

Ayyappa A


Video Answer


2 Answers

Can you please try,

Vehicle.max('id', {where : {'vsr_id': 342 }})
.success(function(vehicle_id){
   ...
})
.error(function(error){
   ...
});
like image 115
Kamrul Avatar answered Oct 10 '22 04:10

Kamrul


That query should be possible with:

Vehicle.findAll({
  attributes: [
     sequelize.fn('MAX', sequelize.col('id'))
  ],
  where: {
    { 'vsr_id': 342 }
  }
});
like image 4
Nilesh Patel Avatar answered Oct 10 '22 03:10

Nilesh Patel