question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Raw queries Model conversion

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
felixfbeckercommented, Sep 27, 2016

That’s simply how raw queries work, they return the result set. You can map it to a model, but not with includes, and you need to alias the column names to the correct properties. Sequelize does this internally as well.

0reactions
hronrocommented, Sep 14, 2017

@janmeier Any news? And why #5685 is closed now?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Raw Queries - Sequelize
By default the function will return two arguments - a results array, and an object containing metadata (such as amount of affected rows,...
Read more >
Sequelize nodejs guidance about raw query conversion to ...
I am new to sequelize and I have given tasks to change sequelize raw queries to ORM and I am confused in so...
Read more >
Performing raw SQL queries | Django documentation
Django gives you two ways of performing raw SQL queries: you can use Manager.raw() ... raw() automatically maps fields in the query to...
Read more >
SQL Queries - EF Core - Microsoft Learn
SQL queries can return regular entity types or keyless entity types that are part of your model. Tip. You can view this article's...
Read more >
Raw queries | Objection.js
To mix raw SQL with queries, use the raw function from the main module. raw works just like the knex's raw method (opens...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found