Gavin
Gavin

Reputation: 7944

Using SQL within attributes

I'm trying to do some calculations with a field and select the value. In my attributes I have:

attributes: ['username', 'last_activity', '(last_activity - 1000) as test']

That doesn't work, since it encloses the SQL in quotes.

How can I do something like this with Sequelize?

Upvotes: 0

Views: 297

Answers (1)

Jan Aagaard Meier
Jan Aagaard Meier

Reputation: 28778

With v2.0.0dev12 you can do:

Model.find({
  attributes: [
    'username', 
    'last_activity',
    // (last_activity - 1000) as test
    Sequelize.literal('(last_activity - 1000) as test')
    // (last_activity - 1000) as `test` - the as part is quoted
    [Sequelize.literal('(last_activity - 1000)'), 'test']
  ]
});

Upvotes: 2

Related Questions