Skip to content

Commit

Permalink
fix: belongsToMany relation withCount
Browse files Browse the repository at this point in the history
  • Loading branch information
kiddyuchina committed May 14, 2024
1 parent c232422 commit 9e4e7ce
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/relations/belongs-to-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,27 @@ class BelongsToMany extends compose(
// this.touch();
// }
// }

getRelationExistenceQuery(query, parentQuery, columns = ['*']) {
if (parentQuery.getQuery()._single.table == query.getQuery()._single.table) {
return this.getRelationExistenceQueryForSelfJoin(query, parentQuery, columns);
}

this.performJoin(query);

return super.getRelationExistenceQuery(query, parentQuery, columns);
}

getRelationExistenceQueryForSelfJoin(query, parentQuery, columns = ['*']) {
const hash = this.getRelationCountHash();
query.select(columns).from(this.related.getTable() + ' as ' + hash);

this.related.setTable(hash);

this.performJoin(query);

return super.getRelationExistenceQuery(query, parentQuery, columns);
}
}

module.exports = BelongsToMany;

0 comments on commit 9e4e7ce

Please sign in to comment.