Ticket #438 (closed defect: fixed)
2 Joins on the same table
| Reported by: | bernhard.slominski@… | Owned by: | hans |
|---|---|---|---|
| Priority: | high | Milestone: | 1.5 Beta 1 |
| Component: | Generator | Version: | 1.2.1 |
| Severity: | major | Keywords: | criteria |
| Cc: | v-dogg |
Description
I have the following challenge: I have a table with 2 joins on the same table. The tables look like this:
<table name="orders">
<column name="id" type="INTEGER" primaryKey="true"/>
<column name="status_lookup_id" type="INTEGER" />
<column name="type_lookup_id" type="INTEGER" />
<foreign-key foreignTable="lookup">
<reference
local="status_lookup_id"
foreign="id"/>
</foreign-key>
<foreign-key foreignTable="lookup">
<reference
local="type_lookup_id"
foreign="id"/>
</foreign-key>
</table>
<table name="lookup">
<column name="id" type="INTEGER" required="true" primaryKey="true"/>
<column name="name" type="VARCHAR" size="40" required="true"/>
<column name="description" type="VARCHAR" size="200" />
</table>
When using a doSelectJoinAll(...) Propel creates the following sql:
SELECT orders.id, orders.status_lookup_id, orders.type_lookup_id, lookup.id,
lookup.name, lookup.description
FROM orders, lookup
WHERE orders.status_lookup_id = lookup.id
AND orders.type_lookup_id = lookup.id
This is of course not working because the two columns status_lookup_id and type_lookup_id have (normally) two different values, so the query always returns an empty resultset.
The right query would be to use aliases thus joining the lookup table twice:
SELECT orders.id, orders.status_lookup_id, orders.type_lookup_id,
l_status.id, l_status.name, l_status.description,
l_type.id, l_type.name, l_type.description
FROM orders, lookup l_status, lookup l_type
WHERE orders.status_lookup_id = l_status.id
AND orders.type_lookup_id = l_type.id
This is of course a bit tricky. The key point is also that I need two different Lookup Objects for the status and the type.
Change History
Note: See
TracTickets for help on using
tickets.