I wanted to know if it's possible to wrap the relationship using the server function jsonb or row_to_json instead of translatting the result in json using python.
When I retrieves the Documentation, I want to have the Image object as a json.
class Documentation(Base):
__tablename__ = 'documentation'
id = Column(UUID, server_default=text("uuid_generate_v4()"), primary_key=True)
url = Column(String, nullable=False)
description = Column(String)
name = Column(String)
image_id = Column(UUID,ForeignKey(Image.id))
image = relationship(Image,backref="image_data",lazy="subquery")
class Image(Base):
__tablename__ = 'image'
id = Column(UUID, server_default=text("uuid_generate_v4()"), primary_key=True)
name = Column(VARCHAR(128), nullable=False)
info = Column(JSONB)
image = Column(BYTEA)
Exemple of the sql request that I want to translate using sqlalchemy:
select doc.*,
row_to_json(image.*) as image
from documentation doc
left join image on doc.image_id = image.id;
SQLAlchemy (current request)
q = Session.query(Documentation).all()
Is it possible to do it using sqlalchemy serverside (on postgresql) ? (for performance issues)
Thanks