Fix lower() error when running on non-string column (#8419)

Signed-off-by: jozefcipa <jozef.cipa@strv.com>
This commit is contained in:
Jozef Cipa 2021-01-21 11:36:17 +01:00 committed by GitHub
parent 7ec9056326
commit a48f9f3327
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -329,9 +329,9 @@ const buildWhereClause = ({ qb, field, operator, value }) => {
case 'nin':
return qb.whereNotIn(field, Array.isArray(value) ? value : [value]);
case 'contains':
return qb.whereRaw('LOWER(??) LIKE LOWER(?)', [field, `%${value}%`]);
return qb.whereRaw(`${fieldLowerFn(qb)} LIKE LOWER(?)`, [field, `%${value}%`]);
case 'ncontains':
return qb.whereRaw('LOWER(??) NOT LIKE LOWER(?)', [field, `%${value}%`]);
return qb.whereRaw(`${fieldLowerFn(qb)} NOT LIKE LOWER(?)`, [field, `%${value}%`]);
case 'containss':
return qb.where(field, 'like', `%${value}%`);
case 'ncontainss':
@ -345,6 +345,14 @@ const buildWhereClause = ({ qb, field, operator, value }) => {
}
};
const fieldLowerFn = qb => {
// Postgres requires string to be passed
if (qb.client.config.client === 'pg') {
return 'LOWER(CAST(?? AS VARCHAR))';
}
return 'LOWER(??)';
};
const findAssoc = (model, key) => model.associations.find(assoc => assoc.alias === key);
module.exports = buildQuery;