Dave
Dave

Reputation: 85

Create a relationship between two db records in a form in Python / Flask / SQLAlchemy

I am desperately trying to do the following:

  1. Capture details of 2 individuals in a Flaskform (i.e. a father and a mother). Each individual is an instance of an Individual class / model and stored in an SQLAlchemy table
  2. When the second individual is added (could be the father or the mother), then a relationship is created with the ids of the two Individuals, in a Parents table. This creates a relationship record between the father and the mother. I'm happy that's all set up OK.

Main app code is below. I want to make sure I can grab both id's at the same time, to create the relationship (hence the print statements).

Yet the first time I add an individual, two print statements output:

New father ID is 1

New mother ID is None  # this is probably expected as I haven't added the second Individual yet

Second time I add an individual, I get:

New father ID is None

New mother ID is 2      # What has happened to the first Individual's ID?

I have tried declaring global variables to write the ids back to but get errors about using a variable before they are declared.

Can anyone help?

from genealogy import app
from flask import render_template, session, redirect, url_for, request
from genealogy import db
from genealogy.models import Individual, Parents
from genealogy.individual.forms import AddIndividual

@app.route("/", methods=["GET", "POST"])
def index():

    form = AddIndividual()

    def fullname(first, last):
        return first + " " + last

    if request.method == "POST":

        new_father = Individual("",None,None)
        new_mother = Individual("",None,None)
        new_child = Individual("",None,None)
        new_partners = Parents(None,None)

        if request.form.get("addfather") == "Add":
            father_forenames = form.father_forenames.data
            father_surname = form.father_surname.data
            father_fullname = fullname(father_forenames, father_surname)

            new_father = Individual(father_surname, father_fullname, father_forenames)
            db.session.add(new_father)
            session["new_father.id"] = new_father.id

            db.session.commit()
            # db.session.flush()

            # if Parents.query.filter_by(father_id=new_father.id, mother_id=new_mother.id):
            #     pass
            # else:
            #     new_partners = Parents(new_father.id, new_mother.id)
            #     db.session.add(new_partners)
            #     db.session.commit()

        if request.form.get("addmother") == "Add":
            mother_forenames = form.mother_forenames.data
            mother_surname = form.mother_surname.data
            mother_fullname = fullname(mother_forenames, mother_surname)

            new_mother = Individual(mother_surname, mother_fullname, mother_forenames)
            db.session.add(new_mother)
            session["new_mother.id"] = new_mother.id

            db.session.commit()
            # db.session.flush()

            # if Parents.query.filter_by(father_id=focus_father.id, mother_id=focus_mother.id):
            #     pass
            # else:
            #     new_partners = Parents(focus_father.id, focus_mother.id)
            #     db.session.add(new_partners)
            #     db.session.commit()

        if request.form.get("addchild") == "Add":
            child_forenames = form.child_forenames.data
            child_surname = form.child_surname.data
            child_fullname = fullname(child_forenames, child_surname)

            new_child = Individual(child_surname, child_fullname, child_forenames)
            db.session.add(new_child)
            focus_person = new_child

            db.session.commit()

        print("New father ID is " + str(session["new_father.id"]))
        print("New mother ID is " + str(session["new_mother.id"]))

        return render_template("home.html", form=form)

        # return render_template("home.html", form=form, focus_father=focus_father, focus_mother=focus_mother,
        #                        focus_person=focus_person, focus_partners=focus_partners)

    return render_template("home.html", form=form)

if __name__ == "__main__":
    app.run(debug=True)

Upvotes: 0

Views: 152

Answers (1)

Dave
Dave

Reputation: 85

Thanks to @van, here's the working code:

    from genealogy import app
    from flask import render_template, session, redirect, url_for, request
    from genealogy import db
    from genealogy.models import Individual, Parents
    from genealogy.individual.forms import AddIndividual
    
    @app.route("/", methods=["GET", "POST"])
    def index():
    
        form = AddIndividual()
    
        def fullname(first, last):
            return first + " " + last
    
        if request.method == "POST":
    
            new_father = Individual("",None,None)
            new_mother = Individual("",None,None)
            new_child = Individual("",None,None)
            new_partners = Parents(None,None)
    
            if request.form.get("addfather") == "Add":
                father_forenames = form.father_forenames.data
                father_surname = form.father_surname.data
                father_fullname = fullname(father_forenames, father_surname)
    
                new_father = Individual(father_surname, father_fullname, father_forenames)
                db.session.add(new_father)

    
                db.session.commit()
                db.session.flush()
                session["new_father.id"] = new_father.id

    
            if request.form.get("addmother") == "Add":
                mother_forenames = form.mother_forenames.data
                mother_surname = form.mother_surname.data
                mother_fullname = fullname(mother_forenames, mother_surname)
    
                new_mother = Individual(mother_surname, mother_fullname, mother_forenames)
                db.session.add(new_mother)

    
                db.session.commit()
                db.session.flush()
                session["new_mother.id"] = new_mother.id
    
    
            if request.form.get("addchild") == "Add":
                child_forenames = form.child_forenames.data
                child_surname = form.child_surname.data
                child_fullname = fullname(child_forenames, child_surname)
    
                new_child = Individual(child_surname, child_fullname, child_forenames)
                db.session.add(new_child)
                focus_person = new_child
    
                db.session.commit()
    
            print("New father ID is " + str(session["new_father.id"]))
            print("New mother ID is " + str(session["new_mother.id"]))
    
            return render_template("home.html", form=form)
    
            # return render_template("home.html", form=form, focus_father=focus_father, focus_mother=focus_mother,
            #                        focus_person=focus_person, focus_partners=focus_partners)
    
        return render_template("home.html", form=form)
    
    if __name__ == "__main__":
        app.run(debug=True)


Upvotes: 1

Related Questions