[英]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 子句。
'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
对象。 所以你去。 希望这个噩梦问题能在未来对其他人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.