我尝试将hasOne
与findAll
方法相关联
如下所示
await db.Symbol.findAll({
where:{
symbol: symbols
},
attributes: ['symbol'],
include: [{
as: 'price',
model: db.Price,
attributes: ['adjClose', 'createdAt']
}]
});
但是我得到了错误的结果,因为sequelize给我的结果如下。
{
symbol: 'SNAP',
price: { adjClose: 22.07, createdAt: 2017-03-10T00:00:00.000Z }
},
{
symbol: 'SNAP',
price: { adjClose: 22.709999, createdAt: 2017-03-09T00:00:00.000Z }
}
但我只需要一个对象,因为我将hasOne关联包含为price
,并且不需要我的'symbol'对象的重复值。
我的hasOne
关联看起来像这样
Symbol.hasOne(models.Price,{
as: "price",
foreignKey:"companyId"
});
有没有办法只选择一个带有包含的项目? 我只是限制子查询,我想这可以帮助,但不幸的是我知道 sequelize不支持它。
希望有人可以帮助我。 提前谢谢!