Global scope of pivotModel not included in whereHas()
See original GitHub issueThis scenario requires following things:
- model with
belongsToMany
relationship usingpivotModel
- pivot model which has a global query scope
The problem: queries using whereHas
are not applying the global scope of a pivot model.
Query generated by the builder:
select * from `users` where exists (select * from `tags` inner join `user_tags` on `tags`.`id` = `user_tags`.`tag_id` where `users`.`id` = `user_tags`.`user_id`)
should be:
select * from `users` where exists (select * from `tags` inner join `user_tags` on `tags`.`id` = `user_tags`.`tag_id` where `users`.`id` = `user_tags`.`user_id` and `user_tags`.`active` = 1)
Package version
6.1.3
Node.js and npm version
$ node --version
v10.15.2
$ npm --version
6.4.1
Sample Code (to reproduce the issue)
/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
const Model = use('Model')
class Tag extends Model {
}
class User extends Model {
tags () {
return this.belongsToMany('App/Models/Tag')
.pivotModel('App/Models/UserTag')
}
}
class UserTag extends Model {
static boot () {
super.boot()
this.addGlobalScope(
query => {
query.where('active', true)
},
'foo'
)
}
}
// the query:
User.query().whereHas('tags').first()
BONUS (a sample repo to reproduce the issue)
https://github.com/radmen/bug-adonis-where-has-pivot-scope
Setup:
npm i
adonis migration:run
adonis seed
DEBUG=knex:query adonis serve
Open: http://localhost:3333/
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Remove a specific global scope in a whereHas model call in ...
I was able to solve this by overriding the relating methods. I've created a trait for ease of use.
Read more >Eloquent ORM - Laravel - The PHP Framework For Web Artisans
When querying a model that uses soft deletes, the "deleted" models will not be included in query results. Forcing Soft Deleted Models Into...
Read more >Calling scope on pivot model in many to many relationship
I have a model for the pivot ThingUser, and a scope scopeScope on ThingUser. The scope is complicated and i do not want...
Read more >Ordering database queries by relationship columns in Laravel
We're not trying to simply order the results of the relationship itself. ... is by using a global scope on the relationship model...
Read more >Relationships - October CMS - 2.x
The scope parameter can also refer to a static method. ... However, if the foreign key on the Phone model is not user_id...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@thetutlage thanks! It solves this issue.
Btw, I have a question - is it possible to use scopes of
pivotModel
inwhereHas
query? It could be quite helpful.A quick example (using soft-deletes package):
@radmen Thanks for sharing a sample repo, it helps. Want to try it from Github as see if latest commit fixes the issue for you or not?