Aditya
Aditya

Reputation: 11

How do I select a value as a column in sequelize

How do I perform sql query such as this SELECT 'OLD' AS CUSTOM_COLUMN FROM TABLE in sequelize?

Upvotes: 1

Views: 3391

Answers (2)

Roger Ramirez
Roger Ramirez

Reputation: 176

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
    });

Upvotes: 4

Tom Jardine-McNamara
Tom Jardine-McNamara

Reputation: 2608

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/)

Upvotes: -1

Related Questions