user2738698
user2738698

Reputation: 578

How to pass multiple conditions to python peewee's join's on argument?

I was wondering how to do the following SQL query in peewee.

SELECT
    farmers.fruit_count,
    vendors.fruit_count,
FROM
    farmers
INNER JOIN
    vendors
ON
    farmers.location = vendors.location
AND
    farmers.alliance = vendors.alliance
AND
    farmers.fruit_count > 0

Is ON-AND-AND-... even doable in peewee? There is not much documentation regarding the on argument. Must I resort to where()? I would assume the query would look something like the following, assuming the on argument takes the same form as what where() takes:

Farmers.select(Farmers.fruit_count,
               Vendors.fruit_count)
       .join(Vendors,
             join_type=JOIN.INNER,
             on=(Farmers.location == Vendors.location,
                 Farmers.alliance == Vendors.alliance,
                 Farmers.fruit_count > 0)

Upvotes: 2

Views: 2629

Answers (1)

coleifer
coleifer

Reputation: 26245

Just use & to join the conditions:

on=((Farmers.location == Vendors.location) &
    (Farmers.alliance == Vendors.alliance) &
    (Farmers.fruit_count > 0))

Upvotes: 3

Related Questions