Skip to content

Request: joining subqueries #399

Closed
Closed
@alvra

Description

I'd like to be able to construct queries like

SELECT *
FROM t1
INNER JOIN (
    SELECT * FROM t2
) ON t2.rel_id = t2.id

so that's joining a subquery (peewee.SelectQuery) instead of a table (peewee.Model or peewee.ModelAlias). (Of course, this makes more sense if the subquery is a bit more complex, but you get the idea.)
Currently (2.2.5), when trying this, it doensn't work.

>>> Related.select().join(Thing, on=Thing.related==thing)  # works as expected
<class 'peewee.Related'> SELECT ... FROM "related" AS t1 INNER JOIN "thing" AS t2 ON (t2."related_id" = t1."id") []
>>> Related.select().join(Thing.select(), on=Thing.related==thing)  # fails unfortunately
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "peewee.py", line 1677, in __repr__
    instance = collected_models[key]
  File "peewee.py", line 2059, in sql
    return query
  File "peewee.py", line 1254, in generate_select
    left_field = field
  File "peewee.py", line 1203, in calculate_alias_map
    if node._negated:
  File "peewee.py", line 1194, in <lambda>
    unknown = True
AttributeError: 'SelectQueryWrapper' object has no attribute '_meta'

When looking through the code it seems Peewee doesn't support this, and I couldn't find a simple way to make this work.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions