-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Query builder yields invalid SQL with JOINED inheritance type on PostgreSQL #6464
Comments
Well, so I had another look at this issue myself, and the SQL that probably should be generated is SELECT ... FROM posts p0_ INNER JOIN (authors a2_ INNER JOIN users u1_ ON a2_.id = u1_.id) ON (p0_.authorId = u1_.id) ... mind the parentheses around the CTI JOIN. if ($targetClass->isInheritanceTypeJoined()) {
$ctiJoins = $this->_generateClassTableInheritanceJoins($targetClass, $joinedDqlAlias);
// If we have WITH condition, we need to build nested joins for target class table and cti joins
if ($withCondition) {
$sql .= '(' . $targetTableJoin['table'] . $ctiJoins . ') ON ' . $targetTableJoin['condition'];
} else {
$sql .= $targetTableJoin['table'] . ' ON ' . $targetTableJoin['condition'] . $ctiJoins;
}
} else {
$sql .= $targetTableJoin['table'] . ' ON ' . $targetTableJoin['condition'];
} If I apply this also to the code path through diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php
index b39280b39..98c30c195 100644
--- a/lib/Doctrine/ORM/Query/SqlWalker.php
+++ b/lib/Doctrine/ORM/Query/SqlWalker.php
@@ -890,7 +890,7 @@ public function walkRangeVariableDeclaration($rangeVariableDeclaration)
);
if ($class->isInheritanceTypeJoined()) {
- $sql .= $this->_generateClassTableInheritanceJoins($class, $dqlAlias);
+ $sql = '(' . $sql . $this->_generateClassTableInheritanceJoins($class, $dqlAlias) . ')';
}
return $sql;
@@ -1136,10 +1136,6 @@ public function walkJoin($join)
$conditions[] = '(' . $this->walkConditionalExpression($join->conditionalExpression) . ')';
}
- $condExprConjunction = ($class->isInheritanceTypeJoined() && $joinType != AST\Join::JOIN_TYPE_LEFT && $joinType != AST\Join::JOIN_TYPE_LEFTOUTER)
- ? ' AND '
- : ' ON ';
-
$sql .= $this->walkRangeVariableDeclaration($joinDeclaration);
// Apply remaining inheritance restrictions
@@ -1157,7 +1153,7 @@ public function walkJoin($join)
}
if ($conditions) {
- $sql .= $condExprConjunction . implode(' AND ', $conditions);
+ $sql .= ' ON ' . implode(' AND ', $conditions);
}
break; ... the sql statement from above is produced. And it works fine with PostgreSQL as well as MySQL. Let me know if that's the way to go, then I'll happily provide a pull request... |
This change is causing unnecessary bracets if there is no other JOIN in the brackets. This is invalid syntax (at least for SQL Server). Please have a look at the fix #6812 |
Given a simple set of entities (adapted to a simpler domain here):
Post
belongs to anAuthor
which derives fromUser
Post
andAuthor
is not mapped (for reasons, as the real case is less simple)If I then instanciate and use a QueryBuilder like this:
it generates the following SQL
Which is valid for MySQL, however is not valid for PostgreSQL (tried with 9.6)
PostgreSQL seems to require an
ON
clause with each and everyINNER JOIN
(interestingly as opposed toLEFT JOIN
). This is, the following query works with it:Pushing all conditions to
WHERE
also works on PostgreSQL:I haven't yet dug deeper, mainly as I never dived into Doctrine ORM code ... please let me know if you agree on this being a bug, ... I so far have no idea where it goes wrong, but let me know if I can support you, maybe by providing test code or start digging ...
The text was updated successfully, but these errors were encountered: