How do I perform sql query such as this SELECT 'OLD' AS CUSTOM_COLUMN FROM TABLE
in sequelize?
Asked
Active
Viewed 3,082 times
1

Aditya
- 11
- 1
- 2
-
Does this answer your question? [How do I select a column using an alias](https://stackoverflow.com/questions/32649218/how-do-i-select-a-column-using-an-alias) – gdfgdfg Oct 14 '20 at 09:07
2 Answers
4
You can use sequelize.literal for that.
const response = await YourModel.findAll({
where: {
...attributes
},
attributes: [
'column_name_on_model',
[sequelize.literal("'OLD'"), 'CUSTOM_COLUMN'], //custom column name
],
raw: true
});

Roger Ramirez
- 176
- 7
-1
If you've set up a model for your table, you can alias the column in the model definition:
{
CUSTOM_COLUMN: {
type: Sequelize.STRING,
field: 'OLD'
}
}
And then limit a find query to that column:
MyModel.find all({ attributes: ['CUSTOM_COLUMN'] })
(See http://sequelize.readthedocs.io/en/v3/docs/models-definition/)

Tom Jardine-McNamara
- 2,418
- 13
- 24