E
E
Evgeny Petrov2020-07-26 14:00:52
ORM
Evgeny Petrov, 2020-07-26 14:00:52

Why is Sequelize COUNT not included in the selection?

Hello!

I have a strange case. I make a selection in the database using Sequelize:

const result = await Orders.findAll({
                attributes: ['college', [Sequelize.fn('COUNT', Sequelize.col('id')), 'count_col']],
                where: {
                    category: 7,
                    status: 30,
                    downloaded: 0
                },
                group: ['college']
            })

I get this SQL:
SELECT `college`, COUNT(`id`) AS `count_col` FROM `Orders` AS `Orders` WHERE `Orders`.`category` = 7 AND `Orders`.`status` = 30 AND `Orders`.`downloaded` = 0 GROUP BY `college`;

Next I do:
result.map(current => console.log(current.count_col))


here count_col = undefined, although if you do: I get:

result.map(current => console.log(current))


Orders {
  dataValues: { college: 12, count_col: 11 },     // ЗДЕСЬ count_col ЕСТЬ!
  _previousDataValues: { college: 12, count_col: 11 },
  _changed: {},
  _modelOptions: {
...

Can anyone explain what the hell is going on here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2020-07-26
@kazsat

This property is not described in your model, so you need to get it like this
current.dataValues.count_col;
Advice: do not use sequilize, it is monstrous, try knex

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question