一对多呼叫关系参考SQLAlchemy Flask

时间:2018-11-08 03:36:49

标签: python sql api flask flask-sqlalchemy

这是对一对多

的引用

ChurchModel 下,我想查询所有具有home_assembly = church_name的牧师(我想返回'ministers': all_ministers

父母:

class ChurchModel(db.Model):
    __tablename__ = 'churches'

 church_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(80))
    ministers = db.relationship('MinisterModel', backref="home_assembly")

def __init__(self, name):
        self.name = name

def json(self):

    return {'name': self.name, 'church_id': self.church_id, 'ministers': all_ministers}

孩子:

class MinisterModel(db.Model):
    __tablename__ = 'ministers'

    minister_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(80), nullable=False)
    home_assembly_id = db.Column(db.Integer, db.ForeignKey('churches.church_id'))

def __init__(self, name, home_assembly):
        self.name = name
    def json(self):
        return {'name': self.name, 'minister_id': self.minister_id, 'home_assembly:': self.home_assembly}

0 个答案:

没有答案