DDC-1435: Exception thrown when generating SQL from a DQL subselect where the entity has a foreign key as a primary key #2061

Closed
doctrinebot opened this Issue Oct 18, 2011 · 8 comments

2 participants

@doctrinebot

Jira issue originally created by user phekmat:

If an entity has a foreign key as part of its primary key and you try to query that entity in a subselect clause in DQL, an exception is thrown in ClassMetadataInfo->getQuotedColumnName. The problem seems to be that it's only considering the field mappings rather than both the field and association mappings when it's looking for the correct column name.

Example entities:

  • Article(id primary key, title, content)
  • ArticleTag(articleid, tag, primary key (articleid, tag))

Example DQL: select art from Article art where exists (select tag from ArticleTag tag where tag.article = art)

@doctrinebot

Comment created by @beberlei:

Verified:

    /****
     * @group [DDC-1435](http://www.doctrine-project.org/jira/browse/DDC-1435)
     */
    public function testForeignKeyAsPrimaryKeySubselect()
    {
        $this->assertSqlGeneration(
            "SELECT s FROM Doctrine\Tests\Models\DDC117\DDC117Article s WHERE EXISTS (SELECT r FROM Doctrine\Tests\Models\DDC117\DDC117Reference r WHERE r.source = s)",
            ""
        );
    }

This however is not possible anyways, since you cannot have "r" in a SimpleSelectExpression when it resolves to multiple columns. You can try to select just one arbitrary field, for example."SELECT r.foobar FROM .."

@doctrinebot

Comment created by phekmat:

Would that just be a documentation issue then? I didn't see that restriction in the grammar, but I may have overlooked it. I issued a pull request for this issue assuming that an association mapped field is valid input for getQuotedColumnName.

Selecting a single field does work fine.

@doctrinebot

Comment created by @beberlei:

Did you touch this in your latest changes to SimpleSelectExpression guilherme?

@doctrinebot

Comment created by @guilhermeblanco:

Hi,

@beberlei No. Issue was totally unrelated.
Please merge my commit to 2.1.X.

The issue was caused by the identifier iteration was considering that @Id fields are always fields, never associations. You may see that in method getQuoteColumnName.

Solution was to create a new method called getQuotedIdenitiferColumnNames and consume it in SqlWalker.
This addresses the issue.

Committed patch and test case as of: 0ec2cc5

Cheers,

@doctrinebot

Issue was closed with resolution "Fixed"

@doctrinebot

Comment created by @beberlei:

Merged into 2.1.x

@doctrinebot

Comment created by @beberlei:

This issue is referenced in Github Pull-Request GH-159
#159

@doctrinebot

Comment created by @beberlei:

A related Github Pull-Request [GH-159] was opened
doctrine/dbal#159

@doctrinebot doctrinebot added this to the 2.1.3 milestone Dec 6, 2015
@doctrinebot doctrinebot closed this Dec 6, 2015
@doctrinebot doctrinebot added the Bug label Dec 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment