DDC-1081: Unnecessary JOIN when selecting ManyToMany/Join Table by ID. #1352

Open
opened 2026-01-22 13:11:24 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (Mar 27, 2011).

Originally assigned to: @billschaller on GitHub.

Jira issue originally created by user putgeminmouth:

With the schema:

Image
    @Id
    $id

Tag
    @Id
    $Id

Tag_Image
    @Id
    @OneToOne(targetEntity="Tag")
    @JoinColumn(name="tag")
    $tag

    @Id
    @OneToOne(targetEntity="Image")
    @JoinColumn(name="image")
    $image

Given the following DQL,

    SELECT img
    FROM Image 
    LEFT JOIN img.tags tag
    WHERE tag.id=:tag

Doctrine Generates this SQL

    SELECT i0_.id AS id1 
    FROM Image i0_ 
    LEFT JOIN Tag*Image t2* 
        ON i0*.id = t2*.image 
    LEFT JOIN Tag t1_ 
        ON t1*.id = t2*.tag 
    WHERE t1_.id = 37

Which unncessarily joins against Tag, given that the foreign key Tag.id is also found in Tag_Image.tag.

Originally created by @doctrinebot on GitHub (Mar 27, 2011). Originally assigned to: @billschaller on GitHub. Jira issue originally created by user putgeminmouth: With the schema: ``` Image @Id $id Tag @Id $Id Tag_Image @Id @OneToOne(targetEntity="Tag") @JoinColumn(name="tag") $tag @Id @OneToOne(targetEntity="Image") @JoinColumn(name="image") $image ``` Given the following DQL, ``` SELECT img FROM Image LEFT JOIN img.tags tag WHERE tag.id=:tag ``` Doctrine Generates this SQL ``` SELECT i0_.id AS id1 FROM Image i0_ LEFT JOIN Tag*Image t2* ON i0*.id = t2*.image LEFT JOIN Tag t1_ ON t1*.id = t2*.tag WHERE t1_.id = 37 ``` Which unncessarily joins against Tag, given that the foreign key Tag.id is also found in Tag_Image.tag.
admin added the Improvement label 2026-01-22 13:11:24 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1352