How to retrieve all elements from a table with a specific attribute from the relationship with other table in SQLALCHEMY

Viewed 26

I'm using SQLALCHEMY in a FastApi application and I have two tables that I want to list all elements from one making join and getting only a specific row from the other, the tables follow below:

class BranchOffice(Base):
BranchOfficeId = Column(Integer, primary_key = True, index = True, autoincrement=False)
AreaId = Column(Integer)
AreaDescription = Column(String(45))
Uf = Column(String(2))
Sellers = relationship("Seller",backref="BranchOffice")



class Seller(Base):
SellerId = Column(Integer, primary_key = True, index = True, autoincrement=False)
SellerName = Column(String(60))
OriginId = Column(Integer, ForeignKey("Origin.OriginId"))
BranchOfficeId = Column(Integer, ForeignKey("BranchOffice.BranchOfficeId"))

I've tried : sellers = db.query(Seller, BranchOffice.AreaDescription).all()

But it produces this output:

[
  {
    "Seller": {
      "OriginId": 1,
      "SellerName": "teste name 1",
      "SellerId": 222,
      "BranchOfficeId": 555
    },
    "AreaDescription": "teste"
  },
  {
    "Seller": {
      "OriginId": 1,
      "SellerName": "teste name 2",
      "SellerId": 222,
      "BranchOfficeId": 555
    },
    "AreaDescription": "teste novo"
  }
]

I wanna "AreaDescription" inside the object Seller like this:

{
    "Seller": {
      "OriginId": 1,
      "SellerName": "teste name 2",
      "SellerId": 222,
      "BranchOfficeId": 555,
      "AreaDescription": "teste novo"
    },
    
  }

Does someone have a solution?

0 Answers
Related