0

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

2

1 Answer 1

0

Right now these are two different tables. If you wanted to use jsonb instead of just translating into JSON as a property of Documentation, you'd need to move all of the image data into a new column under Documentation using one of the compatible json types. Since this is a 1:1 relationship, they belong in the same table anyways.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.