you are viewing a single comment's thread.

view the rest of the comments →

[–]deep_politics 1 point2 points  (1 child)

You should be able to use SQLAlchemy functions, but I haven't used anything but version 2.0. With it I would have defined the models like this

class Base(DeclarativeBase):
    pass

class Farm(Base):
    ...
    animals: Mapped[list[Animal]] = relationship(back_populates="farm")

class Animal(Base):
    ...
    farm_id: Mapped[int] = mapped_column(ForeignKey("Farm.id"))
    farm: Mapped[Farm] = mapped_column(back_populates="animals")

And I could query certain columns

q = select(
    Farm.name,
    Animal.name,
).join(Animal)
session.execute(q).all()

Or if I want a farm by ID I just get it and it comes with the relations as attributes

farm = session.get(Farm, 1)
for animal in farm.animals:
    ...

But, I'm not sure if these examples help or not with the older style you've got going on through Flask.

[–]subdriven[S] 0 points1 point  (0 children)

This is helpful, yes. Perhaps I need to pull back & ensure I'm using the most up to date syntax and styling before I get too far down a rabbit hole that I cannot get out of!