1 year ago

#386676

test-img

St3g4n0

Multiple backref cannot access object property on One to Many relationship SQLAlchemy

In the following snippet I put a template similar to my implementation. Where we have a "Grandparent" object with a OneToMany relationship with the "Parent" object and this the same relationship with the "Child" object.

class Grandparent(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False)
    name = db.Column(db.String(50), nullable=False, unique=True)
    parents = db.relationship('Parent', backref='grandparent')

class Parent(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False)
    name = db.Column(db.String(50), nullable=False, unique=True)
    grandparent_id = Column(Integer, ForeignKey('grandparent.id'))
    children = db.relationship('Child', backref='parent')

class Child(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False)
    name = db.Column(db.String(50), nullable=False, unique=True)
    parent_id = Column(Integer, ForeignKey('parent.id'))

What I want to do is get a list through the child object with information from the father and grandfather. The jinja code is as follows:

<table>
    <thead>
        <tr>
            <th>Children name</th>
            <th>Parent name</th>
            <th>Grandparent name</th>
        </tr>
    </thead>
    <tbody>
        {% for child in children %}
        <tr>
            <td>{{ child.name }}</td>
            <td>{{ child.parent.name }}</td>
            <td>{{ child.parent.grandparent.name }}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>

I get the following error:

jinja2.exceptions.UndefinedError: 'None' has no attribute 'grandparent'

It fails when performing the 2 backref, I don't know how it should be done correctly, since in the official guide it is implemented in this way.

def add(model, **kwargs):
    instance = model(**kwargs)
    db.session.add(instance)
    db.session.commit()

def getAll(model):
    data = model.query.all()
    return data

I use the function above to get all the objects from the database or add a new one. So when the endpoint is reached I do the following:

@bp.route('/example/')
def example():
    children = getAll(Child)
    return render_template('index.html', children=children)

Solution: all objects must have associated parent.

flask

sqlalchemy

jinja2

flask-sqlalchemy

0 Answers

Your Answer

Accepted video resources