Repository navigation
Open
Labels
dialect: mysqlFor issues and PRs. Things that involve MySQL (and do not involve all dialects).status: awaiting responseFor issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action
Description
What are you doing?
I'm trying to fetch the latest price from the ProductPrice table and ordering the products using "this" fetched price - involves two kinds of ordering.
- For Latest Price - The ordering of the associated returned prices of a product (single product
have manyprices) - Sorting in Descending order Onidto get the latest price, withlimit:1. - Then re-ordering of the multiple products by price
I am trying to query the most recent price and then order the multiple products whether in ascending or descending order.
Query
models.Product.findAndCountAll({
//THIS LINE PRODUCES ERROR
order: [models.ProductPrice, "price", "DESC"],
include: [
//BRINGS THE CORRECT LATEST PRICE
{ model: models.ProductPrice, order: [["id", "DESC"]], limit:1 },
]
})
.then(data=>{
res.json({data.rows})
}What is happening?
Error
SequelizeDatabaseError: Unknown column 'ProductPrices.price' in 'order clause'
Models
product.js
module.exports = function(sequelize, DataTypes) {
var Product = sequelize.define(
"Product",
{
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
title: {
type: DataTypes.STRING(80),
allowNull: true,
},
},
{
tableName: "product",
},
);
Product.associate = function(models) {
Product.hasMany(models.ProductPrice, { foreignKey: "product_id" });
};
return Product;
};product-price.js
module.exports = function(sequelize, DataTypes) {
var ProductPrice = sequelize.define(
"ProductPrice",
{
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
},
product_id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
},
price: {
type: DataTypes.INTEGER(11),
allowNull: true,
}
},
{
tableName: "product_price",
},
);
ProductPrice.associate = function(models) {
ProductPrice.belongsTo(models.Product, { foreignKey: "product_id" });
};
return ProductPrice;
};What do you expect to happen?
Expected data.
// ordered by price + latest price
Products: [
{
id: 47,
title: "Product #3",
createdAt: "2019-02-19T09:35:02.000Z",
updatedAt: "2019-02-19T09:35:02.000Z",
ProductPrices: [
{
id: 1,
product_id: 47,
price: 15000,
createdAt: "2019-02-19T09:35:02.000Z",
updatedAt: "2019-02-19T09:35:02.000Z",
},
],
},
{
id: 73,
title: "Product #7",
createdAt: "2019-02-19T09:35:02.000Z",
updatedAt: "2019-02-19T09:35:02.000Z",
ProductPrices: [
{
id: 3,
product_id: 47,
price: 14000,
createdAt: "2019-02-19T09:35:02.000Z",
updatedAt: "2019-02-19T09:35:02.000Z",
},
],
},
{
id: 21,
title: "Product #2",
createdAt: "2019-02-19T09:35:02.000Z",
updatedAt: "2019-02-19T09:35:02.000Z",
ProductPrices: [
{
id: 2,
product_id: 47,
price: 13000,
createdAt: "2019-02-19T09:35:02.000Z",
updatedAt: "2019-02-19T09:35:02.000Z",
},
],
},
]Environment
- mysql
Sequelize version: 5.6.1
Metadata
Metadata
Assignees
Labels
dialect: mysqlFor issues and PRs. Things that involve MySQL (and do not involve all dialects).status: awaiting responseFor issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action
Type
Projects
- StatusShow more project fieldsNo status
Activity
papb commentedon Aug 1, 2019
Hello! Can you please post:
What SQL query was generated by Sequelize
What SQL query you wanted it to generate instead
Thanks!
papb commentedon Aug 1, 2019
Also, can you please test this with another dialect? This is important to know whether the problem is dialect-specific or not.
You can instantiate an in-memory SQLite instance very easily:
npm install sqlite3new Sequelize('sqlite::memory:')You can instantiate a local postgres instance very easily:
.exeand thenCREATE DATABASE testdb;(don't close the.exe)docker pull postgres && docker run --rm --name pg -d -p 5432:5432 postgresdocker exec -t pg psql -U postgres -c 'CREATE DATABASE testdb;'new Sequelize('postgres://postgres@localhost:5432/testdb')github-actions commentedon Nov 11, 2021
This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. 🙂