Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global scope of pivotModel not included in whereHas() #429

Closed
radmen opened this issue Mar 9, 2019 · 7 comments
Closed

Global scope of pivotModel not included in whereHas() #429

radmen opened this issue Mar 9, 2019 · 7 comments
Assignees

Comments

@radmen
Copy link
Contributor

radmen commented Mar 9, 2019

This scenario requires following things:

  • model with belongsToMany relationship using pivotModel
  • 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/

@thetutlage
Copy link
Member

@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?

@radmen
Copy link
Contributor Author

radmen commented Mar 12, 2019

@thetutlage thanks! It solves this issue.

Btw, I have a question - is it possible to use scopes of pivotModel in whereHas query? It could be quite helpful.

A quick example (using soft-deletes package):

User.query().whereHas('posts', qb => {
  qp.withTrashed();
});

@lokendrajoshi54
Copy link

@thetutlage and @radmen Thanks it solved my issue. Though I need to test if I can use withTrashed() method in whereHas query.

@radmen
Copy link
Contributor Author

radmen commented Mar 21, 2019

@thetutlage I know that you're a busy person, so apologies for bothering you. Could you check my previous comment it? Thank you.

@thetutlage
Copy link
Member

In the example you shared, you expect withTrashed method to exist on the PivotModel?

@radmen
Copy link
Contributor Author

radmen commented Mar 22, 2019

In this particular case, it's a scope method. I was expecting that it will be possible to call it as a "regular" query scope methods.

// edit
It's part of PivotModel :)

@josh231101
Copy link

Any updates on this? @radmen @thetutlage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants