mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
DDC-1958: pager produces wrong results on postgresql #2473
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @doctrinebot on GitHub (Jul 30, 2012).
Originally assigned to: @beberlei on GitHub.
Jira issue originally created by user mvrhov:
The query build by pager to get the subset of PKs to fetch produces wrong results on potgresql (and probably any database), that conforms to the SQL standard. The standard says, that if you wish to have the results in specific order, then you have to specify that by using an ORDER BY clause. If such a clause is not present the database can return the results in whatever order it sees fit.
Testcase fixtures:
Passing f.e.
to pager produces SQL like this modified for readability
Now there is nothing wrong with this modified query per se, but there is no ORDER BY clause in the outer query so according to the standard the DB can choose whatever order it seems fit. Now mysql chooses the same order, but postgresql does not and it's probably not the only DB doing so.
If you are interested in the results, this is the output I'm seeing:
I and my coworker came to the standard compliant solution it was also tested on the dataset above on both postgresql and mysql and it produced equal results. We have found only one corner case this won't work and IMHO that can't be fixed. The problem is when you do a sort on a field from a table that is in 1:n relation to the main table.. e.g tables posts and tags, where one post can have a multiple tags and you want your results sorted by a tag.
Recipe for a correct query is:
so if I take the example from above the SQL should look like this:
@doctrinebot commented on GitHub (Jul 30, 2012):
@doctrinebot commented on GitHub (Nov 8, 2012):
Comment created by sylfel:
I reproduce same problem with Postgres 7.4, Doctrine 2.3 whereas with doctrine 2.2, all is fine
Hope there'll a fix in next doctrine version
@doctrinebot commented on GitHub (Nov 12, 2012):
Issue was closed with resolution "Fixed"
@doctrinebot commented on GitHub (Apr 9, 2013):
Comment created by rkolbe:
http://www.doctrine-project.org/jira/browse/DDC-1800 This relates.
I just published a PR for an Oracle fix, but your solution appears to work for Oracle as well (issue is the same).
@doctrinebot commented on GitHub (Aug 6, 2013):
Comment created by warxcell:
Same bug occurs in MariaDB 5.5.
I commented out check for PostgreSQL and it works fine. Can you fix it for MariaDB too? Thanks :)
@doctrinebot commented on GitHub (Aug 21, 2014):
Comment created by xguiga:
I make a ORDER BY with a HIDDEN field and this still happen to me! Like this:
$qb->addSelect('CASE WHEN p.description LIKE :description THEN 0 ELSE 1 END HIDDEN relevance')->addOrderBy('relevance');
This order by is ignored causing the same error!
In this line https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Query/SqlWalker.php#L1310
you don't add to ResultSetMapping so when you verifying in function preserveSqlOrdering the field doesn't exists!
@doctrinebot commented on GitHub (Sep 16, 2014):
Comment created by elyobo:
I've just been bitten by the "corner case" described above, "the problem is when you do a sort on a field from a table that is in 1:n relation to the main table.. e.g tables posts and tags, where one post can have a multiple tags and you want your results sorted by a tag.".
This is a pretty significant bug, as the end result is that data that should come back from the query doesn't. While there probably isn't a good universal workaround, the MySQL behaviour before this was already correct because the outer query was returning the ids in the same order as the internal query (even though it isn't required to by the standard). Is it possible to avoid having this apply to MySQL so that it doesn't introduce an additional bug in an attempt to fix an issue that doesn't apply to that platform anyway?
@doctrinebot commented on GitHub (Sep 16, 2014):
Comment created by mvrhov:
@Liam As you can se above the same applies to mariadb and if you look at the issues on the githubs doctrine project page you'll see that there is the same problem with newer mysql releases. AS I've written above. this corner case cannot be solved.
@doctrinebot commented on GitHub (Sep 16, 2014):
Comment created by elyobo:
Thanks Miha. I couldn't find this on the github page so didn't realise that it was affecting some newer MySQL releases (it didn't seem to affect mine, 5.5). If that's the case, then as you point out it can't even be fixed for MySQL.
Perhaps the lack of support could be more explicit instead? If you attempt to use the paginator with two FROM tables then a RuntimeException is thrown, if we did the same when the ORDER BY conditions applied to tables joined via a 1:m relationship then at least users would know that things were going wrong rather than getting strangely unpredictable results.