legacy-docs icon indicating copy to clipboard operation
legacy-docs copied to clipboard

whereExists example from docs not working

Open MZanggl opened this issue 7 years ago • 1 comments

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.

MZanggl avatar Dec 28 '18 23:12 MZanggl

The working example should looks like this:

await Database.from('users').whereExists(function () {
      this.from('accounts').whereRaw('users.id = accounts.user_id')
})

lukaszflorczak avatar Jul 15 '20 15:07 lukaszflorczak