sequelize-builddynamicwhere子句和'op.or'

ljo96ir5  于 2021-09-23  发布在  Java
关注(0)|答案(0)|浏览(218)

赏金6天后到期。此问题的答案有资格获得+100声誉奖励。dmikester1希望引起更多关注这个问题:

我知道这是可能的,也不应该太难,但我就是找不到任何好的例子。
我用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子句的模型。

'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' }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题