[英]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.