How can I get a pandas dataframe with relationships from a sqlalchemy query

Viewed 96

I have a sqlalchemy table with relationships:

class Scenaries(Base):
    id = Column(Integer, primary_key=True)
    code = Column(String(10))
    name = Column(String(150))

class Materials(Base):
    id = Column(Integer, primary_key=True)
    scenaryId = Column(Integer, ForeignKey(Scenaries.id))

    scenary = relationship(Scenaries, lazy='subquery')

I can convert the sql statement into a pandas dataframe:

query = session.query(Materials)
content  = pd.read_sql(query.statement, query.session.bind)

but I don't know how to create a pandas dataframe that includes the relationship data

The result I would like to obtain would be something like an array field with the information of the scenary related to the main table

Thank you in advance

0 Answers
Related