'How to add a column that includes the row's ID in Python SQLAlchemy?

I'm creating a web app that has a comments section where each comment is displayed beneath any comment that it's a reply to. Each comment should have its own path based on this. The reply tree might look something like this:

"1"
"1.3"
"2"
"2.4"
"2.4.6"
"5"
"5.7"
"8"
"8.9"
"8.10"

I've already written code that orders these kinds of string right way, I'm just stuck on figuring out to to assign the correct path to each comment. My class looks like this:

from flask_sqlalchemy import SQLAlchemy

class Comment(db.Model):
    __tablename__ = 'comments'
    id = db.Column(db.Integer, primary_key=True)
    user = db.Column(db.String(100))
    parent = db.Column(db.Text)    #This is the path of the parent post; null if the comment is not a reply.
    body = db.Column(db.Text, nullable=False)
    path = db.Column(db.Text)    #This should be equal to the ID if the parent is null. Otherwise, it should be "parent + '.' + id"

I've spent a while writing this app and I'm sure this is my last big hurdle. Any help would be greatly appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source