mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
Postgres: Alias cannot be used in select/where/order-by #6174
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 @iluuu1994 on GitHub (Feb 10, 2019).
Bug Report
Summary
I apologize if there's already an issue for this, I couldn't find anything related.
Doctrine allows you to use aliases in various components of the query:
SELECTcomponentsWHEREclauseORDER BYclauseConsider the following rather useless query:
Doctrine happily compiles this to the following SQL:
This works on MySql but it's actually non-standard syntax. Postgres, Sql Server and others do not support this. The way to do this on those platforms is to use a CTE:
https://stackoverflow.com/a/8095413/1320374
Since CTEs are not supported in Doctrine there's currently no way to execute such a query.
Expected behavior
It would of course be awesome if Doctrine would automatically create a CTE when necessary. But I understand that that would be difficult to implement.
This seems like a difficult problem to fix. Is this a known issue? Is there a workaround?
@Ocramius commented on GitHub (Feb 11, 2019):
I think the DQL component is resolving aliases globally within the context of the entire query, without checking where an alias came from. Does it work with subquery aliases, for example?
@Tony-Esales commented on GitHub (Feb 11, 2019):
@iluuu1994
1 - Postgres allows you to use alias in the order by portion. I think it's a standard because it's allowed in Oracle and MySQL too;
2 - Sorry for my ignorance but, what CTE stands for? Can you explain, please?
@iluuu1994 commented on GitHub (Feb 11, 2019):
@Tony-Esales
Right, the
ORDER BYcould use the index of the alias. Unfortunately that will not work forSELECTandWHEREclauses.@iluuu1994 commented on GitHub (Feb 11, 2019):
@Tony-Esales
It stands for common table expression. It's just a different name for the
WITHstatement.@iluuu1994 commented on GitHub (Feb 11, 2019):
@Ocramius
Yeah, looks like it. The following DQL:
generates this SQL:
But again, results in a SQL that
id_0doesn't exist.