Skip to content

fix postgres eager loading alias bug - #19552

Merged
dereuromark merged 1 commit into
5.xfrom
5.x-fix-pg-unqouted-alias-bug-with-eager-loading
Jul 20, 2026
Merged

dereuromark merged 1 commit into
5.xfrom
5.x-fix-pg-unqouted-alias-bug-with-eager-loading

Conversation

@LordSimal

@LordSimal LordSimal commented Jul 20, 2026

Copy link
Copy Markdown
Member

Closes #19551

Fix PostgreSQL subquery eager loading for hasMany / belongsToMany associations when quoteIdentifiers is disabled by quoting the derived-table select alias in the generated join condition.

PostgreSQL always treats quoted select aliases as case-sensitive. The subquery loader emitted aliases like:

AS "Authors__id"

but referenced them in the join condition as:

Authors.Authors__id

With auto-quoting disabled, PostgreSQL folds that to lowercase and fails with column authors.authors__id does not exist.

@LordSimal LordSimal added this to the 5.4.0 milestone Jul 20, 2026
@LordSimal
LordSimal requested review from ADmad and markstory July 20, 2026 17:14
@LordSimal

Copy link
Copy Markdown
Member Author

@Mapiiik may you test this PR if it fixes your problem?
You can adjust your composer.json to

"cakephp/cakephp": "dev-5.x-fix-pg-unqouted-alias-bug-with-eager-loading as 5.4.0",

and perform a composer update -W to test it

@Mapiiik

Mapiiik commented Jul 20, 2026

Copy link
Copy Markdown

Thank you very much @LordSimal!

Tested the PR and it looks good.
The join condition that previously failed in PostgreSQL is now correctly quoted.

Before (broken):
... Customers ON Emails.customer_id = Customers.Customers__id ...

After (fixed):
... Customers ON Emails.customer_id = Customers."Customers__id" ...

Works as expected now.

@dereuromark
dereuromark merged commit a20fe7c into 5.x Jul 20, 2026
15 checks passed
@dereuromark
dereuromark deleted the 5.x-fix-pg-unqouted-alias-bug-with-eager-loading branch July 20, 2026 19:39
@othercorey

othercorey commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

@LordSimal Does cake always auto-quote the select alias outside of subqueries?

@LordSimal

Copy link
Copy Markdown
Member Author

@othercorey Select Aliases have always been quoted for PostgreSQL but this specific bug was for sub-queries specifically where this logic was not yet applied.

markstory added a commit that referenced this pull request Sep 13, 2026
Add new cases to DriverFeatureEnum for recently added driver specific
behavior related to subquery association loading. I'd like to avoid
having driver specific logic in the ORM layer. We have
Driver::supports() which allows us to model this driver specific
behavior like we do for other driver/dialect specific feature support.

Refs #19596
Refs #19552
markstory added a commit that referenced this pull request Sep 15, 2026
…9619)

* Add DriverFeatureEnum for recently added driver specific behavior

Add new cases to DriverFeatureEnum for recently added driver specific
behavior related to subquery association loading. I'd like to avoid
having driver specific logic in the ORM layer. We have
Driver::supports() which allows us to model this driver specific
behavior like we do for other driver/dialect specific feature support.

Refs #19596
Refs #19552

* Apply suggestions from code review

Co-authored-by: othercorey <[email protected]>

---------

Co-authored-by: othercorey <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

4 participants