在sequelize的where(或)条件中指定列名
Specify column name in where (or) condition in sequelize
如何在下面的代码中添加或条件 source 列。
基本上我是在多列中搜索文本。以下是包含 user.name 时抛出的错误。
UnhandledPromiseRejectionWarning: SequelizeDatabaseError: \Unknown column 'flights.user.name' in 'where clause'
下面是代码。
const conditionObj = {
[Op.or]: [
{
source: {
[Op.like]: `%${text}%`
}
},
{
destination: {
[Op.like]: `%${text}%`
}
},
{
"user.name": {
[Op.like]: `%${text}%`
}
}
]
};
const list = await db.flights.findAndCountAll({
where: conditionObj,
attributes: [
"id", "source", "destination"
],
include: [
{
association: "user",
attributes: ["id", "name", "mobile", "email"]
},
]
});
对于关联模型,尝试用 $
包裹字段名称,如下所示:
{
"$user.name$": {
[Op.like]: `%${text}%`
}
}
如何在下面的代码中添加或条件 source 列。
基本上我是在多列中搜索文本。以下是包含 user.name 时抛出的错误。
UnhandledPromiseRejectionWarning: SequelizeDatabaseError: \Unknown column 'flights.user.name' in 'where clause'
下面是代码。
const conditionObj = {
[Op.or]: [
{
source: {
[Op.like]: `%${text}%`
}
},
{
destination: {
[Op.like]: `%${text}%`
}
},
{
"user.name": {
[Op.like]: `%${text}%`
}
}
]
};
const list = await db.flights.findAndCountAll({
where: conditionObj,
attributes: [
"id", "source", "destination"
],
include: [
{
association: "user",
attributes: ["id", "name", "mobile", "email"]
},
]
});
对于关联模型,尝试用 $
包裹字段名称,如下所示:
{
"$user.name$": {
[Op.like]: `%${text}%`
}
}