Jérôme Pigeot
Jérôme Pigeot

Reputation: 2151

how to pass a not like operator in a sqlalchemy ORM query

I've got a query:

MyModel.query.filter(Mymodel.name.contains('a_string'))

I need to do the same query but with the negation (a not like operator) but didn't find any operator matching my need in the SQLAlchemy documentation.

Is there any way to do it without using the sql part of SQLAlchemy???

Upvotes: 78

Views: 57792

Answers (2)

Jossy
Jossy

Reputation: 999

There is now a notlike() method. Couldn't find it in the docs but it exists!

MyModel.query.filter(Mymodel.name.notlike('%a_string%'))

Upvotes: 16

Maxim Sloyko
Maxim Sloyko

Reputation: 15876

Just negate the filter:

MyModel.query.filter(sqlalchemy.not_(Mymodel.name.contains('a_string')))

Upvotes: 118

Related Questions