gpt4 book ai didi

node.js - 使用 Sequelize 分组

转载 作者:行者123 更新时间:2023-12-03 22:17:07 24 4
gpt4 key购买 nike

我有以下类结构:

enter image description here

我正在尝试按 Sequelize 分组和计算每个选择从 Cup_Selections 表中获得的标题数量等于下面键入的查询:

enter image description here

我使用 Sequelize 的 Node 查询如下:

  async index(req, res) {
const champions = await Champion.findAll({
attributes: ['id', 'cupselection_id'],
include: [
{
group: ['selection_id', 'champion.id'],
raw: true,
model: CupSelection,
as: 'cupselection',
attributes: [
'id',
'cup_id',
[fn('count', col('selection_id')), 'vezes'],
],
include: [
{
model: Selection,
as: 'selection',
attributes: ['id', 'country'],
},
],
},
],
});

return res.json(champions);
}

但是向我显示以下错误:
(node:28230) UnhandledPromiseRejectionWarning: SequelizeDatabaseError: column "Champion.id" must appear in the GROUP BY clause or be used in an aggregate function

我该如何解决?

最佳答案

我是这样解决的:

  async index(req, res) {
const { page = 1 } = req.query;

const champions = await Champion.findAll({
limit: 5,
offset: (page - 1) * 5,
order: [[fn('count', col('cupselection.selection.id')), 'DESC']],
attributes: [[fn('count', col('cupselection.selection.id')), 'vezes']],
group: ['cupselection.selection.id', 'cupselection.selection.logo.id'],
raw: true,
include: [
{
model: CupSelection,
as: 'cupselection',
attributes: [],
include: [
{
model: Selection,
as: 'selection',
attributes: ['id', 'country'],
include: [
{
model: File,
as: 'logo',
attributes: ['id', 'path', 'url'],
},
],
},
],
},
],
});

return res.json(champions);
}

关于node.js - 使用 Sequelize 分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61568552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com