javascript - Counting relationship in sequelize returnings 1 instead of 0
问题描述
I have the following test case for sequelize where I am attempting to return the count of a relationship on a model.
test('Tag with file count', async () => {
const tagOne = await db.Tag.create({
label: 'One'
})
const tagTwo = await db.Tag.create({
label: 'Two'
})
const tagThree = await db.Tag.create({
label: 'Three'
})
const tagFour = await db.Tag.create({
label: 'Four'
})
const tagFive = await db.Tag.create({
label: 'Five'
})
const fileOne = await db.File.create()
const fileTwo = await db.File.create()
await fileOne.setTags([tagOne, tagTwo, tagFour])
await fileTwo.setTags([tagOne, tagTwo, tagThree])
const output = await db.Tag.findAll({
attributes: {
include: [
[Sequelize.fn('COUNT', 'Files.id'), 'fileCount']
]
},
include: [
{
model: db.File,
as: 'files',
attributes: [],
duplicate: false
}
],
group: 'Tag.id',
order: [
[Sequelize.literal('`fileCount`'), 'DESC']
]
})
expect(output.length).toBe(5)
expect(output.find(tag => tag.label === tagOne.label).dataValues.fileCount).toBe(2)
expect(output.find(tag => tag.label === tagTwo.label).dataValues.fileCount).toBe(2)
expect(output.find(tag => tag.label === tagThree.label).dataValues.fileCount).toBe(1)
expect(output.find(tag => tag.label === tagFour.label).dataValues.fileCount).toBe(1)
expect(output.find(tag => tag.label === tagFive.label).dataValues.fileCount).toBe(0)
})
For tagFive, I expected the column fileCount to be 0 but it returns 1. For others it seems to return the correct file count.
Is there an option missing in findAll or a bug?
Edit
I get the following query generated
Executing (default): SELECT `Tag`.`id`,
`Tag`.`label`,
`Tag`.`slug`,
`Tag`.`createdAt`,
`Tag`.`updatedAt`, COUNT('Files.id') AS `fileCount`,
`files->FileTags`.`createdAt` AS `files.FileTags.createdAt`,
`files->FileTags`.`updatedAt` AS `files.FileTags.updatedAt`,
`files->FileTags`.`FileId` AS `files.FileTags.FileId`,
`files->FileTags`.`TagId` AS `files.FileTags.TagId`
FROM `Tags` AS `Tag`
LEFT OUTER JOIN `FileTags` AS `files->FileTags`
ON `Tag`.`id` = `files->FileTags`.`TagId`
LEFT OUTER JOIN `Files` AS `files`
ON `files`.`id` = `files->FileTags`.`FileId`
GROUP BY `Tag`.`id` ORDER BY `fileCount` DESC;
Though there's so many alias and I am not comfortable with joins.
解决方案
i made a few changes into something that would work in my database with a similar structure... This did return me some 0 count rows, however this is postgres i am running it on, and you are clearly using something different
SELECT tag.id,
COUNT(story.id) AS fileCount
FROM tag
LEFT OUTER JOIN story_tag
ON tag.name = story_tag.tag
LEFT OUTER JOIN story
ON story.id = story_tag.story_id
GROUP BY tag.id ORDER BY fileCount DESC;
推荐阅读
- ios - 动画单元格时滚动(UITableview)时未检测到触摸
- visual-studio-2017 - 在 Visual Studio 2017 中将项目添加到侧边栏
- java - 为什么登录 Facebook 帐户时用户电子邮件为空,但登录 Google 帐户时用户电子邮件不为空?
- python-3.x - 尽管驱动程序位于 /usr/local/bin 中,但 Selenium “无法找到一组匹配的功能”
- python - 如何使用 Keras 创建一个从表格数据训练的神经网络?
- ms-word - 使用 DDE 连接到 Word 检查 SAS 中是否存在书签的方法
- javascript - 在 Javascript 中使用类进行封装
- python - Django 框架是否容易受到本地文件包含(LFI)和远程文件包含(RFI)的影响?
- flask - 从烧瓶中的基本模板继承时将脚本src设置为动态值
- javascript - Code Igniter 上的动态相关下拉列表国家和州