coleifer/peewee — pull request #141 by arielrossanigo (2012-12-19 21:18:39+00:00)
Title: IN operator and subqueries

This patch allows you to make a query with an IN operator where the subquery return another field than the "id".

For example, I have this two models:

```python
class Customer(Model):
cust_number = IntegerField()
name = CharField()
....

class AccountLog(Model):
name = CharField()
internal_number = IntegerField() # this number is filled with the cust_number
.....
````

and I want to obtain all the Customers who doesn't have an account created.
This query resolve the problem

```python
created_accounts = AccountLog.select(AccountLog.internal_number)
q = Customer.select().where(~(Customer.cust_number << created_accounts))
```

With this patch, the resulting SQL is:

```SQL
SELECT t1."id", t1."cust_number", t1."name"
FROM "customer" AS t1
WHERE NOT (t1."cust_number" IN (
SELECT t2."internal_number"
FROM "accountlog" AS t2))
```

Sorry for my English....