4

I am trying to include hasOne association with findAll method like below

await db.Symbol.findAll({
    where:{
        symbol: symbols
    },
    attributes: ['symbol'],
    include: [{
        as: 'price',
        model: db.Price,
        attributes: ['adjClose', 'createdAt']
    }]
});

but I got wrong result, because sequelize giving me result like below.

{ 
  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 } 
}

but I need only one object , because I have include my hasOne assocation as price and don't need duplicated values of my 'symbol' object.

my hasOne association looks like this

Symbol.hasOne(models.Price,{
    as: "price",
    foreignKey:"companyId"
});

Is there a way to select only one item with include? I nee just limit in subquery which I guess can help, but as I know unfortunately sequelize doesn't support it.

Hope someone can help me. Thanks in advance!

Aren Hovsepyan
  • 1,947
  • 2
  • 17
  • 45

0 Answers0