Typescript Sequelize Use the column of a child for where

Viewed 1503

I want to use here a column of the table Class5 using this line:

'$Class5.time$': filter

But the error manager says:

TS2769: No overload matches this call.
Overload 1 of 2, '(this: ModelStatic, options: NonNullFindOptions): Promise', gave the following error.     
Type '{ id: string; '$Class5.time$': filter; }' is not assignable to type 'WhereOptions'.       
Object literal may only specify known properties, and ''$Class5.time$'' does not exist in type 'WhereOptions'.   
Overload 2 of 2, '(this: ModelStatic, options?: FindOptions): Promise', gave the following error.     
Type '{ id: string; '$Class5.time$': { [sequelize.Op.col]: string; }; }' is not assignable to type 'WhereOptions'.       
Object literal may only specify known properties, and ''$Class5.time$'' does not exist in type 'WhereOptions'.

Here's my full code:

rejectOnEmpty: undefined,
include: [ 
    { 
        model: Class1, 
        include: [ 
            { 
                model: Class2, i
                nclude: [ 
                    { 
                        model: Class3,
                        where: { },
                        include: [
                            { 
                                model: Class4, 
                                include: [
                                    { 
                                        model: Class5, 
                                        where: { }, 
                                        include: [
                                            { 
                                                model: Class6, 
                                                required: true 
                                            }
                                        ] 
                                    }
                                ] 
                            }
                        ] 
                    }
                ] 
            }
        ] 
    }
],
where: { 
    id: id, 
    '$Class5.time$': filter 
},

I've found something nearly similar on this question, but it didn't work for me.

1 Answers

I had the same problem but I couldn't get these $$ literals to work directly inside the where property. Instead, I could find two other workarounds. You may use any of these solutions to achieve the same behavior:

Workaround 1 - Without string literals

where: {

    [Op.and]: [

        sequelize.where(

           sequelize.literal('table1.columnA'), { [Op.eq] : 123 },

        ),

       sequelize.where(

            sequelize.literal('table1.columnB'), { [Op.eq] : null },

        ),

        sequelize.where(

            sequelize.literal('table2.columnA'), { [Op.eq] : null },

        )

    ]

}

Workaround 2 - With string literals

where: {

    columnA: 123,
    columnB: { [Op.eq]: null },
    // * Note the single item inside Op.or.
    // * The inner item will be ANDed with rest of the conditions.
    // * I could not use $table2.columnA$ directly inside the where property,
    //   as it throws the same error as mentioned in the OP.
    [Op.or]: {

       '$table2.columnA$': { [Op.eq] : null }

    },

}
Related