Skip to content

Raw queries Model conversion #6536

@jdnichollsc

Description

@jdnichollsc

What you are doing?

I'm using raw queries to get data between some tables.

//models/order.js
module.exports = function (sequelize, DataTypes) {

    var Order = sequelize.define('order', {
        orderDescription: {
            type: DataTypes.STRING(250),
            allowNull: false,
            field: "order_description"
        },        
        siteId: {
            type:DataTypes.STRING(12),
            allowNull: false,
            field: "site_id"
        }
    },
    {
        timestamps: true,
        underscored: true,
        freezeTableName: true
    });

    return Order;
};

//Woks perfect
models.order.findOne({ where: where }).then(function(orderDB){
    /*
    order.toJSON() === {
       orderDescription: "my order",
       siteId: "1234"
    }
    */
});

//******* ERROR ******* Getting orders using Raw queries
sequelize.query('SELECT * FROM orders...', { 
   type: models.sequelize.QueryTypes.SELECT,
   model: models.order,
   raw: true
 }).then(function(orders){

  /* 
  orders === [
      {
         order_description: "my order", <== ERROR PROPERTY NAME
         site_id : "1234"
      }
  ]
  */
})

What do you expect to happen?

Convert the query respecting the model using the property names from the Sequelize Model.

What is actually happening?

The query return the column names from the database instead of property names from the Sequelize Model.

Dialect: postgres
Database version: 9.5.3
Sequelize version: 3.23.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions