Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
one to many
#1
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': self.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': self.name, 'minister_id': self.minister_id, 'church': self.church_id}
Reply


Messages In This Thread
one to many - by ifeatu - Nov-21-2018, 09:23 AM
RE: one to many - by Larz60+ - Nov-21-2018, 10:06 AM
RE: one to many - by ifeatu - Nov-22-2018, 02:26 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020