I have a postgres table represented in the sql alchemy like
from sqlalchemy import Column
from sqlalchemy.dialects.postgresql import UUID, JSONB
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class ListingDatabaseDocument(Base):
__tablename__ = 'listing'
uuid = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, unique=True)
doc = Column(JSONB, nullable=False)
My doc jsonb field looks like
{"name": "SOME_NAME", "features": ["BALCONY", "GARAGE", "ELEVATOR"]}
Now I'd like to get all rows where the doc->'features' array contains "ELEVATOR","GARAGE" - in pure sql I do it like
SELECT * FROM listing
WHERE doc -> 'features' @> ANY(ARRAY['["ELEVATOR","GARAGE"]']::jsonb[])
How to achieve this in SqlAlchemy ? I tried something like
from sqlalchemy.dialects.postgresql import JSONB, ARRAY
from sqlalchemy.sql.expression import cast
from sqlalchemy import any_
return session.query(ListingDatabaseDocument).filter(
ListingDatabaseDocument.doc['features'].op('@>')(any_(cast(['ELEVATOR','GARAGE'], ARRAY(JSONB))))
).all()
but it's not working. Thanks for help !