coleifer/peewee — pull request #482 by arrowgamer (2014-12-20 09:35:34+00:00)
Title: Support for filtering by joined table outside of select

Filtering by a joined table that is not part of the select would cause a KeyError when using aggregate_rows.

Consider the following example models:

```python
class Organization(Model):
name = CharField()

class Job(Model):
name = CharField()
organization = ForeignKeyField(Organization)

class Employee(Model):
name = CharField()
organization = ForeignKeyField(Organization)
job = ForeignKeyField(Job)
```

Setup some basic records like so:

```python
testing = Organization.create(name='Testing Co.')
tester = Job.create(name='Tester', organization=testing)
Employee.create(name='Tester Guy', organization=testing, job=tester)
```

The below causes a key error because the second join that is used to filter is not included in the select. The intent of the query below is to eagerly load an organization and all its jobs based on an employee's name, but not to load the employee's data.

```python
(Organization
.select(Organization, Job)
.join(Job)
.switch(Organization)
.join(Employee)
.where(Employee.name == 'Tester Guy')
.aggregate_rows()
.get())
```

Adding the try/except after line 2036 fixes this.

The below causes a key error because the second join that is used to filter is not included in the select. The intent of the query below is to eagerly load an employee and all its jobs based on an organization's name, but not to load the organization's data.

```python
(Employee
.select(Employee, Job)
.join(Job)
.switch(Employee)
.join(Organization)
.where(Organization.name == 'Testing Co.')
.aggregate_rows()
.get())
```

Adding the try/except after line 2020 fixes this.