Search code examples
pythonsqlitepeewee

Peewee: how to select multiple rows where id matches the list?


list_of_ids = "23,55,11,24"
job = Job.select().join(User).where(Job.id IN (list_of_ids))

I want to get all jobs belonging to a specific User where the job id's matching the list of ids provided. However, I keep getting syntax error and I can't find any documentation on this on the Peewee website.

I want to be able to do update and delete operations as well on the list of ids provided.

It would be nice if Peewee could just let me insert a SQL string. I would just do

SELECT job.name FROM user INNER JOIN job ON user.id = job.id 
WHERE job.id IN (23,55,11,24)

Solution

  • You want something like:

    Job.select().join(User).where(Job.id << list_of_ids.split(','))
    

    x << y will do x IN y, where y is a list or query as described here: https://peewee.readthedocs.io/en/latest/peewee/query_operators.html