Sequelize.js Sequelize:使用关联的数据筛选器对数据进行分组

Sequelize.js Sequelize:使用关联的数据筛选器对数据进行分组,sequelize.js,Sequelize.js,我有一个分组数据查询,需要由结果中未包含的关联进行筛选 我尝试这样做时会出错,因为生成的sql不是我所期望的 User.findAndCountAll({ group: [ sequelize.fn( 'date_format', sequelize.col('User.createdAt'), '%Y-%m-%d' ),

我有一个分组数据查询,需要由结果中未包含的关联进行筛选

我尝试这样做时会出错,因为生成的sql不是我所期望的

    User.findAndCountAll({
        group: [
            sequelize.fn(
                'date_format',
                sequelize.col('User.createdAt'),
                '%Y-%m-%d'
            ),
        ],
        attributes: [
            [
                sequelize.fn(
                    'date_format',
                    sequelize.col('User.createdAt'),
                    '%Y-%m-%d'
                ),
                'date',
            ],
            [sequelize.fn('count', 'User.id'), 'count'],
        ],
        order: [sequelize.literal('date DESC')],
        include: [
            {
                model: models.UserSite,
                as: 'sites',
                required: true,
                where: {
                    tenantId: 'project1'
                },
                attributes: [],
            },
        ],
    });
生成的查询是

   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 \'project.User.id\' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by',
     sql: 'SELECT `User`.* FROM (SELECT `User`.`id`, date_format(`User`.`createdAt`, \'%Y-%m-%d\') AS `date`, count(\'User.id\') AS `count` FROM `Users` AS `User` WHERE (`User`.`deletedAt` > \'2019-02-20 10:17:29\' OR `User`.`deletedAt` IS NULL) AND ( SELECT `userId` FROM `UserSites` AS `sites` WHERE (`sites`.`tenantId` = \'project1\' AND `sites`.`userId` = `User`.`id`) LIMIT 1 ) IS NOT NULL GROUP BY date_format(`User`.`createdAt`, \'%Y-%m-%d\') ORDER BY date DESC LIMIT 0, 25) AS `User` INNER JOIN `UserSites` AS `sites` ON `User`.`id` = `sites`.`userId` AND `sites`.`tenantId` = \'project1\' ORDER BY date DESC;' },
  original: 
   {   Error: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'project.User.id' which is not functionally dependent on columns in GROUP B  Y clause; this is incompatible with sql_mode=only_full_group_by


include
不存在时,查询工作正常

Hi在数据库中运行以下查询-

SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
这是一个临时修复,如果数据库服务器重新启动,它将被撤消。每当数据库服务器重新启动时,您将需要添加一个脚本来运行此查询

欲了解更多详情,建议您阅读-

另外,您正在创建一个名为-date的新属性-您不需要在组选项中创建参数增益。您可以直接在组中使用参数选项