I have following Flask-SqlAlchemy schema. A Territory can have multiple AsmStores associated with it and I want to load all the columns of related entity (AsmStore) when I query for Territory. I have tried to configure the relationship loading using many approaches but none seems to have work. I always get just the related store IDs and no other attribute as given below in the resultant JSON.

class AsmStore(Model):
      __tablename__ = "asmstore"
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String(255))
      storelatitude = db.Column(db.String(255))
      storelongitude = db.Column(db.String(255))
      territoryid = db.Column(
            db.Integer, db.ForeignKey("territory.id"), nullable=False
      )


class Territory(Model):
      __tablename__ = "territory"

      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String(255))
      centrallatitude = db.Column(db.String(255))
      centrallongitude = db.Column(db.String(255))
      depotlatitude = db.Column(db.String(255))
      depotlongitude = db.Column(db.String(255))
      ideallatitude = db.Column(db.String(255))
      ideallongitude = db.Column(db.String(255))
      areaid = db.Column(db.Integer, db.ForeignKey("area.id"), nullable=False)
      storelist = db.relationship("AsmStore", backref="territory")

Query:

territory = Territory.query.filter_by(id=territoryid).first()

Resultant JSON:

{
"data": {
    "centrallatitude": "",
    "id": 1,
    "storelist": [
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10,
        11
    ]
}

}

Have I setup the entities incorrectly? If so please advise correction.

0 Answers