Sorting Sequelize:在关联内部排序
型号 product.jsSorting Sequelize:在关联内部排序,sorting,sequelize.js,associations,Sorting,Sequelize.js,Associations,型号 product.js module.exports=函数(续集,数据类型){ var Product=sequelize.define( “产品”, { 身份证:{ 类型:数据类型。整数(10)。无符号, allowNull:错, primaryKey:没错, 自动递增:真, }, 标题:{ 类型:DataTypes.STRING(80), 阿洛诺:是的, }, }, { 表名:“产品”, }, ); Product.associate=功能(型号){ Product.hasMany(mo
module.exports=函数(续集,数据类型){
var Product=sequelize.define(
“产品”,
{
身份证:{
类型:数据类型。整数(10)。无符号,
allowNull:错,
primaryKey:没错,
自动递增:真,
},
标题:{
类型:DataTypes.STRING(80),
阿洛诺:是的,
},
},
{
表名:“产品”,
},
);
Product.associate=功能(型号){
Product.hasMany(models.ProductPrice,{foreignKey:“Product_id”});
};
退货产品;
};
产品价格.js
module.exports=函数(续集,数据类型){
var ProductPrice=sequelize.define(
“产品价格”,
{
身份证:{
类型:数据类型。整数(10)。无符号,
allowNull:错,
primaryKey:没错,
},
产品编号:{
类型:数据类型。整数(10)。无符号,
allowNull:错,
primaryKey:没错,
},
价格:{
类型:数据类型。整数(11),
阿洛诺:是的,
}
},
{
表名:“产品价格”,
},
);
ProductPrice.associate=功能(型号){
ProductPrice.belongsTo(models.Product,{foreignKey:“Product_id”});
};
退货价格;
};
我正在尝试使用两种订购方式从ProductPrice
表中获取最新价格
有许多
价格)-在id
上按降序排序以获取最新价格,限制为:1models.Product.findAndCountAll({
//这条线产生错误
订单:[[models.ProductPrice,“price”,“DESC”]],
包括:[
//带来正确的最新价格
{model:models.ProductPrice,订单:[id”,“DESC]],限额:1},
]
})
。然后(数据=>{
res.json({data.rows})
}
错误
SequelizeDatabaseError:订单中的未知列“ProductPrices.price”
条款'
注意:忽略我使用的是
findAndCountAll
——除非这是问题所在?我认为你应该在include之后使用order,如下所示
models.Product.findAndCountAll({
include: [
//BRINGS THE CORRECT LATEST PRICE
{ model: models.ProductPrice, order: [["id", "DESC"]], limit:1 },
]
//THIS LINE PRODUCES ERROR
order: [models.ProductPrice, "price", "DESC"],
})
.then(data=>{
res.json({data.rows})
}
作为更多参考
编辑
从官方文件复制的行
Company.findAll({ include: [ Division ], order: [ [ Division, 'name' ] ] });
我认为你应该在include之后使用order,就像这样
models.Product.findAndCountAll({
include: [
//BRINGS THE CORRECT LATEST PRICE
{ model: models.ProductPrice, order: [["id", "DESC"]], limit:1 },
]
//THIS LINE PRODUCES ERROR
order: [models.ProductPrice, "price", "DESC"],
})
.then(data=>{
res.json({data.rows})
}
作为更多参考
编辑
从官方文件复制的行
Company.findAll({ include: [ Division ], order: [ [ Division, 'name' ] ] });
请添加您的
产品
和产品价格
模型定义请添加您的产品
和产品价格
模型定义如果这样做有效的话,这将是非常愚蠢的。然而,查询中没有任何更改。只需访问我提供的官方文档链接,并获取订单即可在关联模型中,你必须首先包含该关联。如果这样做有效的话,那将是非常愚蠢的。然而,查询中没有任何更改。只需访问我提供的官方文档链接,并且为了在关联模型中工作,你必须先包含该关联。。