legacy-docs
legacy-docs copied to clipboard
whereExists example from docs not working
https://adonisjs.com/docs/4.1/query-builder#_whereexists
The example for whereExists goes like this
await Database.from('users').whereExists(function () {
this.from('accounts').where('users.id', 'accounts.user_id')
})
But this has the following output
select * from `users` where exists (select * from `accounts` where `users`.`id` = 'accounts.user_id')
As you can see, it puts single quotes around accounts.user_id so it looks for records where users.id is literally accounts.user_id.
If you look at Laravel's whereExists example they actually use a whereRaw here. https://laravel.com/docs/5.7/queries
But personally I think whereColumn would be even better. It seems that Adonis does not support this method though.
Same problem applies to the whereNotExists example.
The working example should looks like this:
await Database.from('users').whereExists(function () {
this.from('accounts').whereRaw('users.id = accounts.user_id')
})