Skip to content

@DefaultScope and @Scopes do not work w/ query symbols #209

Closed
@schnz

Description

@schnz

Minimal working example to reproduce the issue:

Model:

import { Table, Scopes, Model, PrimaryKey, Column } from 'sequelize-typescript';
import { Op } from 'sequelize';

@Table({
    tableName: 'MyTable',
    timestamps: true,
    paranoid: true,
})
@Scopes({myScope: {
    where: { deletedAt: {
        [Op.lte]: Date.now(),
    }},
}})
export class MyModel extends Model<MyModel> {
    @PrimaryKey
    @Column
    public id: number;
}

Code

const db = new Sequelize({
    dialect: 'mysql',
    host: 'localhost', database: '...', username: '...', password: '...',
    logging: false,
});

db.addModels([MyModel]);
const dbSync = db.sync({ force: true })
    .then(() => { console.log("Sync done!"); })
    .catch((e) => { console.log("Sync error", e); });

dbSync.then(() => {
    MyModel
        .scope('myScope')
        .findAll({
            logging:console.log,
            paranoid: false, // Just to keep the query as simple as possible
            // Where query, exactly as in @Scopes
            where: { createdAt: {
                [Op.lte]: Date.now(),
            }}
        });
})

Expected Query:

SELECT `id`, `createdAt`, `updatedAt`, `deletedAt`
FROM `MyTable` AS `MyModel`
WHERE `MyModel`.`createdAt` <= '2017-11-27 13:16:09'
  AND `MyModel`.`deletedAt` <= '2017-11-27 13:16:09'; -- THIS LINE IS WRONG BELOW!

Actual Query:

SELECT `id`, `createdAt`, `updatedAt`, `deletedAt`
FROM `MyTable` AS `MyModel`
WHERE `MyModel`.`createdAt` <= '2017-11-27 13:16:09'
  AND `MyModel`.`deletedAt` = '2017-11-27 13:16:09';

Please note that this does not only apply to Op.lte. Other complex where queries are not possible either (e.g. where: { deletedAt: { [Op.ne]: null } })

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions