简体   繁体   English

Sequelize过滤一对多关系

[英]Sequelize filtering one to many relation

I have a User model and Timeoffs model with one to many relation.我有一个用户 model 和 Timeoffs model 具有一对多关系。 I want get all the users that doesn't have timeoffs in the specified date.我想获取在指定日期没有超时的所有用户。 Right now in the code I'm doing the exact opposite of this - getting only the users with timeoffs in the given date.现在在代码中我正在做与此完全相反的事情 - 只获取在给定日期有超时的用户。 How can I do this in right way?我怎样才能以正确的方式做到这一点? Is it possible to just invert include in that part or should I write a more complex query?是否可以只在该部分中反转包含,或者我应该编写更复杂的查询?

   getQuestionHourUsersByDate (date, timeId) {
        return UsersModel.findAll({
            where: {
                [USERS.COLUMNS.QH_STATUS]: true,
            },
            include: [
                {
                    model: TimeoffsModel,
                    where: {
                        [Operator.and]: {
                            [TIMEOFFS.COLUMNS.PROCEED_STATUS]: true,
                            [TIMEOFFS.COLUMNS.STATUS]: TimeOffEnums.TIMEOFF_REQUEST_STATUS.APPROVED,
                        },
                        [Operator.and]: {
                            [TIMEOFFS.COLUMNS.START_DATE]: {
                                [Operator.lte]: date,
                            },
                            [TIMEOFFS.COLUMNS.END_DATE]: {
                                [Operator.gte]: date,
                            },
                        },
                    },
                },
                {
                    model: RolesModel,
                    where: {
                        [ROLES.COLUMNS.ROLE_GROUP_ID]: 1,
                    },
                    through: {
                        attributes: [],
                        where: {
                            [USER_ROLES.COLUMNS.STATE]: true,
                        },
                    },
                },
                {
                    model: ResponsibilitiesModel,
                    attributes: [
                        RESPONSIBILITIES.COLUMNS.NAME,
                        RESPONSIBILITIES.COLUMNS.RESPONSIBILITY_GROUP_ID,
                        RESPONSIBILITIES.COLUMNS.COLOR_CODE,
                    ],
                    where: {
                        [RESPONSIBILITIES.COLUMNS.RESPONSIBILITY_GROUP_ID]: 2,
                    },
                    required: false,
                    through: {
                        attributes: [],
                        where: {
                            [USER_RESPONSIBILITIES.COLUMNS.ACTIVE]: true,
                        },
                    },
                },
                {
                    model: LanguagesModel,
                    attributes: [
                        LANGUAGES.COLUMNS.CODE,
                        LANGUAGES.COLUMNS.NAME,
                    ],
                },
                {
                    model: UserCalendarsModel,
                    include: [
                        {
                            model: CalendarsModel,
                            where: {
                                [CALENDARS.COLUMNS.START_DATE]: date,
                                [CALENDARS.COLUMNS.ACTIVE]: true,
                            },
                            include: [
                                {
                                    model: QuestionHourSlotsModel,
                                    where: {
                                        [QUESTION_HOUR_SLOTS.COLUMNS.TIME_ID]: timeId,
                                    },
                                },
                            ],
                        },
                    ],
                },
            ],
        });
    }

Unfortunately, I should use Sequelize.literal in where option to add a subquery condition with NOT EXISTS like this:不幸的是,我应该在where选项中使用Sequelize.literal来添加一个NOT EXISTS存在的子查询条件,如下所示:

where: Sequelize.where(Sequelize.literal('NOT EXISTS (SELECT 1 FROM ... WHERE startDate <= @date and endDate >= @date)'), '=', true)

and also to pass a specified date you add to indicate bind option along with main query options like this:并且还要传递一个指定的日期,您添加以指示bind选项以及主要查询选项,如下所示:

 where: {
                [USERS.COLUMNS.QH_STATUS]: true,
            },
 bind: {
   date
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM