node.js - 在 Sequelize 中使用带有内部连接的组给出错误

标签 node.js sequelize.js

我正在尝试编写这个原始查询

SELECT users.status,count(`users`.`id`) AS `count` 
FROM `users` AS `users` 
   INNER JOIN `organization_entries` AS `organizationEntries` ON `users`.`id` = `organizationEntries`.`user_id` 
       AND `organizationEntries`.`organization_id` = '1' 
       AND `organizationEntries`.`type` = '001' 
group by users.status;

在 Sequelize 。

我像这样在 Sequelize 中编写了这个查询
db.users
    .findAll({
      attributes: [
        'status',
        [db.sequelize.fn('COUNT', 'users.id'), 'entries']
      ],
      include: {
        model: db.organizationEntries,
        where: {


          organization_id: req.params.org_id,
          type: '001',

        },
      },
      group: ['users.status']
    }).then(c => {
      console.log(c);

      let active_users = {
        active_users: c,
      }
      return res.send(active_users);

    })
    .catch(next);

但是当我运行这个查询时,我收到了这个错误

code "ER_WRONG_FIELD_WITH_GROUP" errno 1055 sqlState "42000"

sqlMessage "Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'ontro.users.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by"



我无法理解我做错了什么。

最佳答案

If the ONLY_FULL_GROUP_BY SQL mode is enabled, MySQL rejects queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on them.



请阅读 MySQL Handling of GROUP BY

关于node.js - 在 Sequelize 中使用带有内部连接的组给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48724002/

相关文章:

node.js - 在 firebase 函数 package.json 中包含 @firebase/app

javascript - 如何将两个更新请求合并为一个?

sequelize.js - 子子模型上的序列过滤

postgresql - 使用belongsToMany时在sequelize中获取 "model is not associated with other Model"

sequelize.js - sequelize 事务是原子的吗?

node.js - 热衷于在 Passport-jwt 中设置具有不同用户角色的身份验证?

php - 使用 ImageMagick 获取所有客户端字体的列表?

javascript - socket.io 不记录控制台消息

javascript - 强制 javascript 变量作为 "value"传递

node.js - Express Sequelize 返回 true 或 false