Michał Kandulski
Michał Kandulski

Reputation: 111

String literal as the left hand side argument of 'like' operator using SQLAlchemy

Im trying to construct a query using SQLAlchemy which produces something like (in oracle):

select * from users u where 'john' like u.name || '%'

to get names like 'j', 'jo', 'joh' etc.

I suppose there'a something like:

session.query(Users).filter(XXX('john').like(Users.name + '%')).all()

What should I replace XXX with?

Upvotes: 1

Views: 430

Answers (2)

Audrius Kažukauskas
Audrius Kažukauskas

Reputation: 13543

Use literal construct:

session.query(Users).filter(literal('john').startswith(Users.name)).all()

Upvotes: 3

reptilicus
reptilicus

Reputation: 10397

I think its something like this?

Users.query.filter(Users.name.like("%john%")).all()

Upvotes: -1

Related Questions