[英]Sequelize - Build dynamic where clause with 'Op.or'
I had this code block working with Sequelize v5.我将此代码块与 Sequelize v5 一起使用。 But since switching to v6, it seems to be erroring out.但是自从切换到 v6,它似乎出错了。 I am getting the error: Error: Invalid value { customer_id: 'dg5j5435r4gfd' }
.我收到错误: Error: Invalid value { customer_id: 'dg5j5435r4gfd' }
。
And here is the code that creates the where condition block:这是创建 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;
}
I was using the commented code.我正在使用注释代码。 And then I tried the code below that.然后我尝试了下面的代码。 Both are producing the same error.两者都产生相同的错误。 But when I remove all that code from the if block and just put in whereBlock.customer_id = customerID;
但是当我从 if 块中删除所有代码并放入whereBlock.customer_id = customerID;
, then it works fine. ,然后它工作正常。 So I know the issue is how I am constructing the where condition.所以我知道问题是我如何构建 where 条件。
Update: As requested, here is my Sheets
model where the where clause is being run on.更新:根据要求,这是我的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,
}
);
};
And in my index I have this to associate sheets with customers: db.Sheet.belongsTo(db.Customer);
在我的索引中,我将工作表与客户相关联: db.Sheet.belongsTo(db.Customer);
Also here is the full code where the whereBlock
is used, if that helps:如果有帮助,这里也是使用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: So here is what it comes down to: TLDR:所以这归结为:
whereBlock = {
deleted_at: null,
customer_id: customerID,
// [Op.or]: [
// { customer_id: customerID },
// { customer_id: coreCustomerID },
// ],
};
That code above works, but the commented code errors out with: Error: Invalid value { customer_id: '123456' }
上面的代码有效,但注释代码出错: Error: Invalid value { customer_id: '123456' }
OK, this is very weird.好吧,这很奇怪。 But I finally figured out the issue!!但是我终于想通了这个问题!! Was not something I would have thought of, just found it by chance.不是我会想到的,只是偶然发现的。 It was the way I was importing Op
from sequelize
.这是我从sequelize
导入Op
的方式。
import Op from 'sequelize';
So apparently, that Op
object has another object inside it called Op
.很明显,该Op
对象内部有另一个名为Op
对象。 So when I call my [Op.or]
, I instead need to do this: [Op.Op.or]
.所以当我打电话给我的[Op.or]
,我需要这样做: [Op.Op.or]
。
I did try switching my import to import Op.Op from 'sequelize';
我确实尝试将导入切换为import Op.Op from 'sequelize';
and that caused errors.这导致了错误。 Anyone know how I can properly import the inner object?任何人都知道如何正确导入内部对象?
Too bad I can't award myself the bounty to get my points back!太糟糕了,我不能给自己赏金来取回我的积分! ;) ;)
Update: OK, so apparently in my other DB files, I was doing the import differently.更新:好的,显然在我的其他数据库文件中,我以不同的方式进行导入。
export default (db) => {
const Op = db.Sequelize.Op;
That method works to pull in the correct Op
object.该方法可以拉入正确的Op
对象。 So there you go.所以你去。 Hopefully this nightmare issue helps someone else in the future.希望这个噩梦问题能在未来对其他人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.