问题
I am building a conditional ORM query
let defaultWhere ={
isPublic:1
};
//query to build
let query = {
distinct:true,
include:[
{
model:DealsMeta,
where:{
type:'view'
},
as: "totalviews",
attributes: ['id'],
required:false,
},
{
model:DealsTransactions,
as: "totaltransactions",
attributes: ['id'],
required:false
}
]
};
let keywordWhere = {};
//add the search parameter
if(params.keyword!=undefined&¶ms.keyword!=""){
keywordWhere = {
[Op.or]:[
{title: { $like: '%' + params.keyword + '%' }},
{keywords: { $like: '%' + params.keyword + '%' }},
Sequelize.where(Sequelize.fn('concat_ws', Sequelize.col('firstName'), ' ', Sequelize.col('lastName')), {
$like: '%' + params.keyword + '%'
})
]
};
query.include=query.include.concat({ model:User, required:false,attributes:['firstName'] })
}
query.where=Object.assign(defaultWhere,keywordWhere);
but when I add limit and offset the query is broken I got unknown column error
ie
{
distinct:true,
offset:0,
limit:2
}
Returns broken query
UPDATE
Final query
{ distinct: true,
order: [ [ 'idDeal', 'DESC' ] ],
limit: 2,
offset: 0,
include:
[ { model: deals_metadata,
where: [Object],
as: 'totalviews',
attributes: [Array],
required: false },
{ model: deals_transactions,
as: 'totaltransactions',
attributes: [Array],
required: false },
{ model: USER, required: false, attributes: [Array] } ],
where: { isPublic: 1, [Symbol(or)]: [ [Object], [Object], [Where] ] } }
来源:https://stackoverflow.com/questions/50565120/sequelize-limit-and-order-not-work-with-condition-query