This question is similar to SQLAlchemy query where a column contains a substring, but the other way around: I'm trying to query a column containing a string which is a sub-string of another given string. How can I achieve this?
Here is a code example of a database set up using the ORM:
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql import exists
engine = create_engine('sqlite:///:memory:')
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
url = Column(String)
fullname = Column(String)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
session.add_all([
User(url='john', fullname='John Doe'),
User(url='mary', fullname='Mary Contrary')
])
session.commit()
The following works:
e = session.query(exists().where(User.url == 'john')).scalar()
upon which e
has the value True
. However, I would like to do something like
e = session.query(exists().where(User.url in 'johndoe')).scalar()
where in
is in the sense of the __contains__
method of Python's string type. Is this possible?