Objection.js: Querying agains the virtual fields

Created on 7 Sep 2020  Â·  3Comments  Â·  Source: Vincit/objection.js

Hi Everyone
I have added a custom field in my modal like

get fullName() {
    return `${this.firstName} ${this.lastName}`; 
  }

I want to do the query with this custom field. Like

  this.query().select(fields).where(‘fullName’, ‘ilike’, ‘%test%’)

How can i do this in objectionJS ?

Most helpful comment

You need to useraw in where or the whereRaw method.

this
  .query()
  .select(fields)
  .whereRaw(‘("firstName" || ' ' || "lastName") ilike ?', '%test%')

You obviously cannot use the fullName getter in SQL since it's in javascript.

All 3 comments

You need to useraw in where or the whereRaw method.

this
  .query()
  .select(fields)
  .whereRaw(‘("firstName" || ' ' || "lastName") ilike ?', '%test%')

You obviously cannot use the fullName getter in SQL since it's in javascript.

@koskimas I am still getting error as
column "("firstName" || ' ' || "lastName")" does not exist

You are using " around the whole thing which means an identifier in postgres. Make sure you write valid SQL. This is not specific to objection.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

apronin83 picture apronin83  Â·  3Comments

officer-rosmarino picture officer-rosmarino  Â·  4Comments

zuck picture zuck  Â·  4Comments

bsdo64 picture bsdo64  Â·  3Comments

purepear picture purepear  Â·  3Comments