Sequelize - 使用 'Op.or' 构建动态 where 子句

Sequelize - Build dynamic where clause with 'Op.or'

我让这个代码块与 Sequelize v5 一起工作。但是自从切换到 v6 后,它似乎出错了。我收到错误:Error: Invalid value { customer_id: 'dg5j5435r4gfd' }.

这里是创建 where 条件块的代码:

    let whereBlock = {
        deleted_at: null,
    };

    if (args.includeCore) {
        if (customerID !== 'all') {
            // whereBlock[Op.or] = [
            //  { customer_id: customerID },
            //  { customer_id: coreCustomerID },
            // ];
            whereBlock[Op.or] = [];
            whereBlock[Op.or].push({
                customer_id: customerID,
            });
            whereBlock[Op.or].push({ customer_id: coreCustomerID });
        }
    } else {
        whereBlock.customer_id = customerID;
    }

我使用的是注释代码。然后我尝试了下面的代码。两者都产生相同的错误。但是当我从 if 块中删除所有代码并放入 whereBlock.customer_id = customerID; 时,它就可以正常工作。所以我知道问题是我如何构建 where 条件。

更新: 根据要求,这是我的 Sheets 模型,其中 where 子句 运行 on.

'use strict';

export default (sequelize, DataTypes) => {
    return sequelize.define(
        'Sheet',
        {
            id: {
                type: DataTypes.UUID,
                primaryKey: true,
                defaultValue: DataTypes.UUIDV4,
            },
            sheet_name: {
                type: DataTypes.STRING,
                isAlphaNumeric: true,
                required: true,
                allowNull: true,
                len: [3, 80],
            },
            sheet_file_name: {
                type: DataTypes.STRING,
                unique: true,
                isAlphaNumeric: true,
                required: false,
                allowNull: true,
            },
            brand_name: {
                type: DataTypes.STRING,
                unique: false,
                isAlphaNumeric: true,
                required: false,
                allowNull: true,
            },
            customer_id: {
                // fk in customers table
                type: DataTypes.TINYINT(2).UNSIGNED,
                required: true,
                allowNull: false,
            },
            chemical_id: {
                // fk in loads table
                type: DataTypes.SMALLINT.UNSIGNED,
                required: true,
                allowNull: false,
            },
            load_id: {
                // fk in loads table
                type: DataTypes.SMALLINT.UNSIGNED,
                required: true,
                allowNull: false,
            },
            active: {
                type: DataTypes.BOOLEAN,
                required: true,
                allowNull: false,
                defaultValue: true,
            },
            created_at: {
                type: DataTypes.DATE,
            },
            updated_at: {
                type: DataTypes.DATE,
            },
            deleted_at: {
                type: DataTypes.DATE,
            },
        },
        {
            underscored: true,
            paranoid: false,
        }
    );
};

在我的索引中,我将工作表与客户相关联:db.Sheet.belongsTo(db.Customer);

这里还有使用 whereBlock 的完整代码,如果有帮助的话:

const files = await db.Sheet.findAll({
                raw: true,
                attributes: [
                    'sheet_name',
                    'sheet_file_name',
                    ['brand_name', 'brand'],
                    'updated_at',
                    'active',
                    [Sequelize.col('Chemical.name'), 'chemical'],
                    [Sequelize.col('Load.value'), 'load'],
                ],
                include: [
                    {
                        model: db.Load.scope(null),
                        required: true,
                        as: 'Load',
                        attributes: ['value'],
                    },
                    {
                        model: db.Chemical.scope(null),
                        required: true,
                        as: 'Chemical',
                        attributes: ['name'],
                    },
                ],
                // model: model,
                where: whereBlock,
                order: [['active', 'DESC']],
            });

TLDR: 所以归结为:

whereBlock = {
    deleted_at: null,
    customer_id: customerID,
    // [Op.or]: [
    //  { customer_id: customerID },
    //  { customer_id: coreCustomerID },
    // ],
};

上面的代码有效,但注释代码错误:Error: Invalid value { customer_id: '123456' }

好的,这很奇怪。但我终于想通了这个问题!不是我想到的,只是偶然发现的。这是我从 sequelize.

导入 Op 的方式

import Op from 'sequelize';

很明显,Op 对象内部还有另一个对象 Op。所以当我调用 [Op.or] 时,我需要这样做:[Op.Op.or].

我确实尝试将我的导入切换到 import Op.Op from 'sequelize'; 并且这导致了错误。有人知道我如何正确导入内部对象吗?

更新

好的,显然在我的其他数据库文件中,我的导入方式有所不同。

export default (db) => {
    const Op = db.Sequelize.Op;

该方法可以拉入正确的 Op 对象。所以你去吧。希望这个噩梦般的问题在未来对其他人有所帮助。