Raw queries Model conversion
See original GitHub issueWhat 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:
- Created 7 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
@janmeier Any news? And why #5685 is closed now?