我正在使用knex和objection.js来处理我的postgres数据库。我有这个查询涉及三个不同的表和不同的可能过滤器。我遇到的问题是当我使用 .whereRaw 进行左联接和查询时,出现以下错误:错误:缺少表"发布"的 FROM 子句条目如果我改用 .where,它可以工作。但是我需要 whereRaw 使过滤器不区分大小写。
Reader.query(Reader.knex())
.where('Reader.id', '=', readerId)
.eager('[tags, publications.[tags, attributions]]')
.modifyEager('publications', builder => {
if (filter.title) {
const title = filter.title.toLowerCase()
builder.where('Publication.name', 'like', `%${title}%`)
// builder.whereRaw(
// 'LOWER(name) LIKE ?',
// '%' + filter.title.toLowerCase() + '%'
// )
}
if (filter.attribution || filter.author) {
builder.leftJoin(
'Attribution',
'Attribution.publicationId',
'=',
'Publication.id'
)
}
if (filter.author) {
builder
.where('Attribution.normalizedName', '=', author)
.andWhere('Attribution.role', '=', 'author')
}
if (filter.attribution) {
builder.where(
'Attribution.normalizedName',
'like',
`%${attribution}%`
)
if (filter.role) {
builder.andWhere('Attribution.role', '=', filter.role)
}
}
orderBuilder(builder)
builder.limit(limit)
builder.offset(offset)
})
值得称赞的.whereRaw查询是我想做的查询。.where 和 .whereRaw 之间有什么不同,会导致一个工作而不是另一个工作?
我不确定为什么whereRaw
不起作用,必须进行一些调试才能找出答案,但您可以使用ilike
进行不区分大小写的搜索where
例:
builder.where('Publication.name', 'ilike', `%${title}%`)
参考