Ahmad Siavashi
Ahmad Siavashi

Reputation: 999

SQLAlchemy: Filter by objects in a member list

Consider the following classes,

class Person:
    id
    name
    jobs

class Job:
    name
    person
    person_id (FK)

where Person.jobs refers to class Job objects. Now I'd like to perform the following query,

# for a given person p
Job.query.filter(Job.notin_(p.jobs))

Is it possible in SQLAlchemy?

Please not that I do not want to write Job.query.filter(Job.person_id != p.id).

Upvotes: 0

Views: 3314

Answers (1)

TonyMoutaux
TonyMoutaux

Reputation: 357

Nope !

As the documentation about in_ explains, as of version 1.2 of SQLAlchemy, you can not have list of instances of object, only :

  • list of ID
  • empty list
  • select
  • bound params

As of version 1.2, only what you describe as "do not want" is the right answer :

Job.query.filter(Job.id.notin_([j.id for j in person.jobs]))

Upvotes: 1

Related Questions