New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Struggling to Reliably Count Associated Records #5777
Comments
You're counting and grouping on the same column, that would generally be an error. It would be great to have runnable/shareable snippets, doesn't look like runnable.com supports postgres though. |
I don't know 😢 I think one of the reasons I'm confused is because I wrote a query that sortof works: USE sequelize_question_schema;
SELECT
`Poll`.`question` AS Question,
`PollOption`.`id` AS `PollOptionId`,
`PollOption`.`text` AS `PollOptionText`,
COUNT(`Vote`.`PollOptionId`) AS `Votes`
FROM `Polls` AS `Poll`
JOIN `PollOptions` AS `PollOption`
ON `PollOption`.`PollId` = `Poll`.`id`
LEFT OUTER JOIN `Votes` AS `Vote`
ON `Vote`.`PollOptionId` = `PollOption`.`id`
WHERE `Poll`.`id` = 1
GROUP BY `Vote`.`PollOptionId` I say sortof because it works when there's at least one But when there are no I really appreciate your input, @mickhansen. You are probably correct but I'm confused because the above query counts and groups by the same column and works... sortof.
Yeah, that would be ideal. In the mean time, I like to post a Short, Self Contained, Correct (Compilable), Example (SSCCE). It should be possible to download and run mine in Node as-is. What do you think? Thanks again! |
I'm working on a small sscce-template we can put in the project and run with docker. |
I really appreciate that, thank you! |
MySQL is acting weird in this, i haven't tried to count joined rows in mysql in a while (primarily use postgres) and i don't have anymore time to debug this right now. You might want to see if you can make it work with a raw query first and then i can help you convert that to a sequelize call. |
@mickhansen I totally appreciate that. I'm working on the query now. Thanks. |
Woohoo 🎉 In response to you, @mickhansen, I got the query working: USE sequelize_question_schema;
SELECT
`Poll`.`question` AS Question,
`PollOption`.`id` AS `PollOptionId`,
`PollOption`.`text` AS `PollOptionText`,
COUNT(`Vote`.`id`) AS `Votes`
FROM `Polls` AS `Poll`
LEFT JOIN `PollOptions` AS `PollOption`
ON `PollOption`.`PollId` = `Poll`.`id`
LEFT JOIN `Votes` AS `Vote`
ON `Vote`.`PollOptionId` = `PollOption`.`id`
WHERE `Poll`.`id` = 1
GROUP BY `PollOption`.`id` I wanted to have a stab at this myself before asking for help and voila: function queryTestPoll() {
return Poll.findOne({
where: {
id: 1
},
group: ['PollOptions.id'],
attributes: [
'question',
],
include: {
attributes: [
'text',
[connection.fn('COUNT', connection.col('PollOptions.Votes.id')), 'votes']
],
model: PollOption,
include: {
model: Vote
}
}
}).then(function(foundPoll) {
console.log(JSON.stringify({
question: foundPoll.dataValues.question,
options: foundPoll.dataValues.PollOptions.map(function(option) {
return option.dataValues
})
}, null, 2))
})
} {
"question": "What is the best Node.js ORM?",
"options": [
{
"text": "Sequelize",
"votes": 1,
"Votes": [
{
"id": 1,
"PollOptionId": 1
}
]
},
{
"text": "Knex",
"votes": 0,
"Votes": []
}
]
} Really it came down to grouping and counting by the right columns so thanks a lot @mickhansen ❤️. Just in case someone else is struggling with something similar in the future, here are some resources I used:
|
Great :) I tried a similar group but still got weird results. |
What you are doing?
I am building a poll similar to Straw Poll. I have three tables:
Poll
,PollOption
andVote
. Here are their definitions:And here is the EER diagram:
I can create a sample
Poll
, somePollOption
s and aVote
:The problem occurs when I want to query that information back again:
What do you expect to happen?
Based on what I learned from #222 I wanted:
What is actually happening?
But the output was:
votes
is reported to be1
for both records even though I only voted for one of them 😕.If I do not store a
Vote
in the database the result is:... Which is also not what you would expect 😢
Extra
I can make the code work by omitting the
group
andattributes
fields but the code is inefficient:SSCCE
Dialect: mysql
Database version: 15.1
Sequelize version: 3.21.0
The text was updated successfully, but these errors were encountered: