mysql - Sequelize - 查询 Sequelize 方法

标签 mysql sql node.js sequelize.js

我正在将 Sequelize 用于 Node 项目,但在以 Sequelize 方式转换查询时遇到一些问题。

场景:我有 2 个表(UserEvent),通过 UserEvent 表具有 n:m 关系。在UserEvent表中有is_creator属性;我需要的是一个带有事件名称、事件 id 和 is_creator 字段的对象。

示例:

[{
    id: 1,
    name: "Event",
    is_creator: true
}]

这是查询:

SELECT `Event`.`id`,`Event`.`name` as `Name`, `UserEvents`.`is_creator` AS `is_creator` FROM `Events` AS `Event` INNER JOIN (`UserEvents` INNER JOIN `Users` AS `Users` ON `Users`.`id` = `UserEvents`.`UserId`) ON `Event`.`id` = `UserEvents`.`EventId` AND `Users`.`id` = 1;

不幸的是,我无法将其“翻译”为 Sequelize。

实际代码:

var queryOptions: FindOptions = {
            include: [
                {
                    model: db.User,
                    where: {
                        id: user.id
                    }
                }
            ],
        };

执行的查询:

SELECT `Event`.`id`, `Event`.`uuid`, `Event`.`image`, `Event`.`name`, `Event`.`start_at`, `Event`.`end_at`, `Event`.`location`, `Event`.`description`, `Event`.`createdAt`, `Event`.`updatedAt`, `Users`.`id` AS `Users.id`, `Users`.`uuid` AS `Users.uuid`, `Users`.`email` AS `Users.email`, `Users`.`password` AS `Users.password`, `Users`.`firstName` AS `Users.firstName`, `Users`.`lastName` AS `Users.lastName`, `Users`.`activationCode` AS `Users.activationCode`, `Users`.`resetCode` AS `Users.resetCode`, `Users`.`active` AS `Users.active`, `Users`.`dob` AS `Users.dob`, `Users`.`image` AS `Users.image`, `Users`.`createdAt` AS `Users.createdAt`, `Users`.`updatedAt` AS `Users.updatedAt`, `Users.UserEvent`.`uuid` AS `Users.UserEvent.uuid`, `Users.UserEvent`.`is_creator` AS `Users.UserEvent.is_creator`, `Users.UserEvent`.`createdAt` AS `Users.UserEvent.createdAt`, `Users.UserEvent`.`updatedAt` AS `Users.UserEvent.updatedAt`, `Users.UserEvent`.`UserId` AS `Users.UserEvent.UserId`, `Users.UserEvent`.`EventId` AS `Users.UserEvent.EventId` FROM `Events` AS `Event` INNER JOIN (`UserEvents` AS `Users.UserEvent` INNER JOIN `Users` AS `Users` ON `Users`.`id` = `Users.UserEvent`.`UserId`) ON `Event`.`id` = `Users.UserEvent`.`EventId` AND `Users`.`id` = 1;

预先感谢您的合作!

最佳答案

您想限制查询返回的列数吗?

您可以查看attributes选项。您可以指定所需的表字段。

例如,如果您想要用户的 id、电子邮件和名字,则可以这样指定。

var queryOptions: FindOptions = {
        include: [
            {
                model: db.User,
                where: {
                    id: user.id
                },
                attributes: ['id', 'email', 'firstName']
            }
        ],
    };

您可以类似地为 UserEvents 表添加属性选项。

PS:我目前没有足够的声誉来对问题添加评论。否则我就会那么做。

关于mysql - Sequelize - 查询 Sequelize 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40512359/

相关文章:

MySQL REGEXP 选择数字

SQL语法错误?

sql - 如何从 SQL 中选定的数据集创建临时结果集?

php - PHP 中不区分大小写的查询不起作用

node.js - 如何将连接用作具有类型的独立对象?

javascript - 使用 Node.js(watson-developer-cloud 模块)发送 AlchemyData News 查询

mysql - 为什么 "Reset Pooled Connections"选项对 MySQL .NET 连接器不起作用

php - 如何将表连接到此 SQL 代码?

sql - 将 YYYYMM 转换为 MMMYY

javascript - 如何在实例方法中更新 Mongoose 子文档?