Skip to content

Commit

Permalink
fix(relations): apply pivotModel scopes on belongsToMany sub queries
Browse files Browse the repository at this point in the history
fix #429
  • Loading branch information
thetutlage committed Mar 11, 2019
1 parent 73c1071 commit 6cc7012
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Lucid/Relations/BaseRelation.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class BaseRelation {
* @method applyRelatedScopes
*/
applyRelatedScopes () {
this.relatedQuery._applyScopes()
this._applyScopes()
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Lucid/Relations/BelongsToMany.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ class BelongsToMany extends BaseRelation {
this.relatedQuery.$relation.relatedPrimaryKey = relatedPrimaryKey
}

/**
* Returns reference to pivot model (if attached) otherwise
* returns `null`.
*
* @attribute $pivotModel
*
* @return {Model|null}
*/
get $pivotModel () {
return this._PivotModel
}

/**
* Returns the pivot table name. The pivot model is
* given preference over the default table name.
Expand Down Expand Up @@ -167,6 +179,8 @@ class BelongsToMany extends BaseRelation {
_applyScopes () {
if (this.scopesIterator) {
this.scopesIterator.execute(this)
} else {
this.relatedQuery._applyScopes()
}
}

Expand Down
63 changes: 63 additions & 0 deletions test/unit/lucid-belongs-to-many.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2685,4 +2685,67 @@ test.group('Relations | Belongs To Many', (group) => {

assert.deepEqual(postIds, [1, 2])
})

test('apply global scope on pivate model when called withCount', async (assert) => {
class Post extends Model {
}

class PostUser extends Model {
static get table () {
return 'post_user'
}
}

class User extends Model {
posts () {
return this.belongsToMany(Post).pivotModel(PostUser)
}
}

User._bootIfNotBooted()
PostUser._bootIfNotBooted()
Post._bootIfNotBooted()

PostUser.addGlobalScope(function (builder) {
builder.where(`${builder.$pivotModel.table}.deleted_at`, null)
})

let userQuery = null
User.onQuery((query) => (userQuery = query))
await User.query().withCount('posts').fetch()

assert.equal(userQuery.sql, helpers.formatQuery('select *, (select count(*) from "posts" inner join "post_user" on "posts"."id" = "post_user"."post_id" where "users"."id" = "post_user"."user_id" and "post_user"."deleted_at" is null) as "posts_count" from "users"'))
})

test('apply global scope on pivot model when called has', async (assert) => {
class Post extends Model {
}

class PostUser extends Model {
static get table () {
return 'post_user'
}
}

class User extends Model {
posts () {
return this.belongsToMany(Post).pivotModel(PostUser)
}
}

User._bootIfNotBooted()
Post._bootIfNotBooted()
PostUser._bootIfNotBooted()

PostUser.addGlobalScope(function (builder) {
builder.where(`${builder.$pivotModel.table}.deleted_at`, null)
})

let userQuery = null
User.onQuery((query) => (userQuery = query))

await User.query().has('posts').fetch()

assert.equal(userQuery.sql, helpers.formatQuery('select * from "users" where exists (select * from "posts" inner join "post_user" on "posts"."id" = "post_user"."post_id" where "users"."id" = "post_user"."user_id" and "post_user"."deleted_at" is null)'))
})
})

0 comments on commit 6cc7012

Please sign in to comment.