Python: marshmallow doesn't return backref column data

123 views Asked by At

I have two tables as below where SupplierCompany has a relationship with Office and has a backref column referenced in the Office table:


class Office(BaseModel):
    __tablename__ = 'offices'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=False)


class SupplierCompany(BaseModel):
    __tablename__ = 'supplier_companies'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=False)
    office = db.relationship('Office', lazy='subquery', backref='supplier_companies')
    office_id = db.Column(db.Integer, db.ForeignKey('offices.id'))


class OfficeSchema(marshmallow.SQLAlchemyAutoSchema):
    class Meta:
        model = Office
        include_relationships = True
        load_instance = True


    supplier_companies = fields.Nested(SupplierCompanySchema, many=True)


class SupplierCompanySchema(marshmallow.SQLAlchemySchema):
    class Meta:
        model = SupplierCompany
        load_instance = True

When I query the Office row, it shows the supplier_companies field as expected. However, when I dump the Office object, the supplier_companies show empty lists, even though I explicitly mentioned many=True in the OfficeSchema field definition.

office = Office.find(name='London')
print(office)
INFO:app:[Brasy, Brytns]

office_schema = OfficeSchema()
office = office_schema.dump(office)
print(office.supplier_companies)
INFO:app: [{}, {}]

0

There are 0 answers