How to create marshmallow schemas for multiple joined tables / models

Viewed 223

I have 4 models in SQLAlchemy that are linked with foreign keys and relationships have been defined as well. My goal is to make a query that retrieves data of these 4 tables and returns JSON. For putting together the JSON I use marshmallow, however I have not been able to define the schemas in such a way that I can fully retrieve data of model AplAngebot (prsmin) and AplArtikel (prs). As far as I understand I can only provide fields in a marshmallow schema if there is also such an attribute in the model, but how to retrieve the other fields of a linked table? See my models:

class AplArtikel(Base):
    __tablename__ = 'APL_ARTIKEL'
    __table_args__ = {"schema": "AP"}
    key_artikel_pos = Column(Float, primary_key=True)
    ref_angebot = Column(Float, ForeignKey('AP.APL_ANGEBOT.key_angebot'))
    prs = Column(Float, nullable=True)
    offer = relationship('AplAngebot', backref="article")


class AplAngebot(Base):
    __tablename__ = 'APL_ANGEBOT'
    __table_args__ = {"schema": "AP"}
    key_angebot = Column(Float, primary_key=True)
    prsmin = Column(Float, nullable=True)


class AplKampagneZuord(Base):
    __tablename__ = 'APL_KAMPAGNE_ZUORD'
    __table_args__ = {"schema": "AP"}
    key_kampagne_zuord = Column(Float, primary_key=True)
    ref_kampagne = Column(Float, ForeignKey('AP.APL_KAMPAGNE.key_kampagne'), nullable=True)
    ref_angebot = Column(Float, ForeignKey('AP.APL_ANGEBOT.key_angebot'))
    campaign = relationship('AplKampagne', backref="zuord")
    offer = relationship('AplAngebot', backref="zuord")
    
    
class AplKampagne(Base):
    __tablename__ = 'APL_KAMPAGNE'
    __table_args__ = {"schema": "AP"}
    key_kampagne = Column(Float, primary_key=True)
    txt_bezeichnung = Column(String, nullable=True)

And here are Schemas and query:

class ArticleSchema(Schema):
    key_artikel_pos = fields.Float()
    prs = fields.Float()   # how to show this field in result?


class OfferSchema(Schema):
    key_angebot = fields.Float()
    prsmin = fields.Float()   # how to show this field in result?
    article = fields.Pluck(ArticleSchema, 'key_artikel_pos')  # does not work


class ZuordSchema(Schema):
    key_kampagne_zuord = fields.Float(dump_only=True)
    offer = fields.Pluck(OfferSchema, 'key_angebot')


class CampaignSchema(Schema):
    key_kampagne = fields.Float()
    txt_bezeichnung = fields.String()
    zuord = fields.Nested(ZuordSchema, many=True)   
    
    
    campaign_schema = CampaignSchema()
    data = session.query(AplKampagne).all()
    json_object = campaign_schema.dumps(data, many=True)
    return json_object  

Current result:

[
  {
    "zuord": [
      {
        "offer": 13066,
        "key_kampagne_zuord": 14440
      },
      {
        "offer": 12532,
        "key_kampagne_zuord": 14460
      },
      {
        "offer": 13463,
        "key_kampagne_zuord": 14461
      }
    ],
    "key_kampagne": 281,
    "txt_bezeichnung": "texxt"
  }, ...
0 Answers
Related