I am trying to select all the child posts from the post id selected by the user. Below is my model definition for Posts and the query that gives the error in the title. I have tried a few things including aliasing which looked promising but this and several other options tried but all gave errors.
# Post model
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
date_posted = db.Column(db.DateTime(100), nullable=False, default=datetime.utcnow)
content = db.Column(db.Text, nullable=False)
topic = db.Column(db.String(100), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
# thread structure using self-referencing see
# https://docs.sqlalchemy.org/en/14/orm/self_referential.html
parent_post = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=True)
child_posts = db.relationship('Post', backref='parent', lazy="joined", remote_side='Post.id') # background query
# display selected post thread route
@posts.route("/post_thread/<int:post_id>")
def post_thread(post_id):
posts = Post.query.filter(Post.id==post_id).join(Post.id==Post.parent_post).all()
# This works but to get only the selected post: posts = Post.query.where(Post.id==post_id)
return render_template('post_thread.html', posts=posts, topic="Thread")