Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 one to many
Good afternoon, I have been beating my head against a wall with an issue for 2 weeks and was hoping you can help...I'm trying to do a one-to-many relationship...between two entities, Church (as in a church building), and minister (as in leaders of a church), there are multiple ministers for any one church...simple right? I am trying to return the name of a church, the church ID, and the ministers that have that church as a "home assembly". Additionally, when returning ministers (from the MinisterModel) I want to return the Name of the minister, the Minister ID, and the home_assembly (a json object of all the info based on church_id).

Can you PLEASE help with this, I feel like if I get over this hump everything else will make sense.

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='church', lazy='dynamic')
def json(self):
        # What I want --> SELECT * from minister where home_assembly_id == self.church_id
        # Attempt 317: ministers.query().filter(ministers.home_assembly_id == self.church_id)
        return {'name':, 'church_id': self.church_id, 'ministers': **?????*** }
class MinisterModel(db.Model):
    __tablename__ = 'ministers'
    minister_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(80))
    # church = db.relationship('ChurchModel')  # join
    church_id = db.Column(db.Integer, db.ForeignKey('churches.church_id'))
def json(self):
        # Return name, minister_id and minister home church
        return {'name':, 'minister_id': self.minister_id, 'church': self.church_id}
Gribouillis wrote Nov-21-2018, 09:35 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

I fixed for you this time. Please use code tags on future posts.
use a dictionary:
churches = {
    'Church1': {
        'Minister1': {
           'Name': 'whatever',
           'otherstuff': '...'
        'Minister2': ...
    'Church2': {
I figured it out:

def json(self):
    return ('ministers':[minister.json() for minister in self.ministers.all()])


Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)