Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Joins on self referring classes #374

Open
wants to merge 1 commit into
from

Conversation

Projects
None yet
3 participants

fabeat commented Jun 4, 2012

Hi,

I've found a bug where joins on self referring classes do not work correctly using queries. The generated SQL is wrong.

Here a snippet from the test output:

$ phpunit test/testsuite/misc/SelfReferringTest

[...]

.E.E

Time: 0 seconds, Memory: 8.25Mb

There were 2 errors:

1) SelfReferringTest::testBasicQueryWithLeftJoin
PropelException: Unable to execute SELECT statement [SELECT self_referring.ID, self_referring.RELATED_ID, self_referring.NAME FROM  LEFT JOIN `self_referring` ON (self_referring.RELATED_ID=self_referring.ID) WHERE Name LIMIT 1] [wrapped: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOIN `self_referring` ON (self_referring.RELATED_ID=self_referring.ID) WHER' at line 1]

[...]

2) SelfReferringTest::testBasicQueryWithNamedLeftJoinAndInlusionOfRelatedObject
PropelException: Unable to execute SELECT statement [SELECT self_referring.ID, self_referring.RELATED_ID, self_referring.NAME, self_referring.ID, self_referring.RELATED_ID, self_referring.NAME FROM  LEFT JOIN `self_referring` ON (self_referring.RELATED_ID=self_referring.ID) WHERE self_referring.NAME=:p1 LIMIT 1] [wrapped: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOIN `self_referring` ON (self_referring.RELATED_ID=self_referring.ID) WHER' at line 1]

[...]

FAILURES!
Tests: 4, Assertions: 0, Errors: 2.

This pull request passes (merged f2334c5 into 3b15493).

Owner

willdurand commented Jun 7, 2012

Apparently, these tests are ok..

Owner

willdurand commented Jun 22, 2012

Ok there is a real bug, because of this code: https://github.com/propelorm/Propel/blob/master/runtime/lib/util/BasePeer.php#L720.

We can easily determine whether a JOIN is a self referential logic, or not. Then I guess we can generate aliases for each table, so that everything will be ok. What do you think?

fabeat commented Jun 27, 2012

Yes that should be OK. But naming the joined table doesn't fix the Problem too

See testBasicQueryWithNamedLeftJoinAndInlusionOfRelatedObject()

We also need to check whether the the same Object is joined twice in general. Think about a relationship Object1->Object2->Object3

Owner

willdurand commented Jul 24, 2012

Yes, you're right! Not so easy then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment