Hackr.io is hiring Software Developers. Email us your CV on hr@hackr.io

Adonis Js Discussion Forums

There's no such thing as a stupid question. Ask. Discuss. Learn.

Query Builder return null for join result and raw query in AdonisJS

I want to get count result by Database.raw in a select query of the query builder but when my main table is empty, all fields of my query are null except Database.raw result.

Here is my query:

const jobs = await Database.table('jobs')
.leftJoin('job_offers', 'jobs.id', 'job_offers.job_id')
.select([
Database.raw(`'job' as type`),
Database.raw('COUNT(jobs.id) as offers_count'),
'jobs.id', 'title', 'jobs.description', 'jobs.created_at',
])
.limit(limit).offset(offset)

and my query result is:

[
{
"type": "job",
"offers_count": 0,
"id": null,
"title": null,
"description": null,
"created_at": null
}
]

In the above query, the result must be [] when there are not any row in my job table.

What is wrong in my query?

Behnam Azimi 8 months ago
  • Whitelisted Links
;