gpt4 book ai didi

javascript - sequelize scope with concatted where

转载 作者:行者123 更新时间:2023-12-03 22:38:27 27 4
gpt4 key购买 nike

如果不使用范围,以下工作:

where = {};

...
case c: {
where = Sequelize.where(Sequelize.fn('concat', Sequelize.col('firstName'), ' ', Sequelize.col('lastName')), {
like: '% John Doe %'
})
}
Model.findAll({
where: where
})

但不是作为作用域函数:
scopes: {
field: function (field, keyword = '%%') {
let where = {};
switch (field.toLowerCase()) {
case 'id':
where.id = {[Op.like]: keyword};
break;
case 'email':
where.email = {[Op.like]: keyword};
break;
case 'name' :
where = sequelize.where(sequelize.fn('concat', sequelize.col('first_name'), ' ', sequelize.col('last_name')), {
[Op.like]: keyword
});
}
return {
where
}
},
}

收到此错误:

Unknown column 'User.attribute' in 'where clause'


console.log(where) :
Where {
attribute: Fn { fn: 'concat', args: [ [Col], ' ', [Col] ] },
comparator: '=',
logic: { [Symbol(like)]: '%%' }
}

有没有人处理过?

最佳答案

当您需要“Sequelize”时,您的作用域函数是否使用“sequelize”?

...
case 'name' :
where = Sequelize.where(sequelize.fn('concat', Sequelize.col('first_name'), ' ', Sequelize.col('last_name')), {
[Op.like]: keyword
...

关于javascript - sequelize scope with concatted where,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54425055/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com