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
fix: allow where IsNull for ManyToOne relations #9031
Conversation
This doesn't appear to work with OR operations: return this.caseRepo.find({
where: [
{
snf: { id: In(queriedIds) },
},
{
snf: IsNull(),
},
],
}); It appears because the query is doing an INNER JOIN and not a LEFT JOIN for the IN operator. |
@jczacharia this workaround might work for you. |
Thank you for contribution! 🎉 |
* fix: allow where IsNull for ManyToOne relations Closes: typeorm#8890 * fix direct FindOperator queries on MTO relation * fix: allow FindOperator queries on OTO owner column * chore: prettier formating
@LoneRifle i just removed the throw you are mentioning locally, it wouldn't solve your issue. I had a quick test with this change, which made diff --git a/src/query-builder/SelectQueryBuilder.ts b/src/query-builder/SelectQueryBuilder.ts
index d3b01053f..11b5b0ea4 100644
--- a/src/query-builder/SelectQueryBuilder.ts
+++ b/src/query-builder/SelectQueryBuilder.ts
@@ -4321,12 +4321,16 @@ export class SelectQueryBuilder<Entity extends ObjectLiteral>
}
if (InstanceChecker.isFindOperator(where[key])) {
+ const { type } = where[key];
if (
- where[key].type === "moreThan" ||
- where[key].type === "lessThan"
+ type === "moreThan" ||
+ type === "moreThanOrEqual" ||
+ type === "lessThan" ||
+ type === "lessThanOrEqual"
) {
- const sqlOperator =
- where[key].type === "moreThan" ? ">" : "<"
+ const baseOperator = /^moreThan/.test(type) ? ">" : "<"
+ const sqlOperator = baseOperator + (/OrEqual$/.test(type) ? '=' : '')
+
// basically relation count functionality
const qb: QueryBuilder<any> = this.subQuery()
if (relation.isManyToManyOwner) { Probably the best is if you open a new issue, and create a PR for that issue. The PR probably has the best chance to get merged if you thoroughly test Also one could argue that the Find operators |
@pixtron - I got it to My condition change felt pretty hacky though, so I did a more invasive change similar to yours (with
|
Fixes: #8890
Description of change
Allows to query ManyToOne relations for records that do not have any relation (IS NULL).
Pull-Request Checklist
master
branchnpm run format
to apply prettier formattingnpm run test
passes with this changeFixes #0000