python - flask-sqlalchemy. multilevel join and and sort -
posts table:
class post(usermixin,db.model): __tablename__ = "posts" id = db.column(db.integer,primary_key= true) timestamp = db.column(db.string(20),index = true) author = db.column(db.integer,db.foreignkey("users.id")) post = db.column(db.string(500),index = true) comments = db.relationship("comment",backref = "post_id", lazy = "joined")
comments table:
class comment(usermixin,db.model): __tablename__ = "comments" id = db.column(db.integer,primary_key = true) comment_author_id = db.column(db.integer,db.foreignkey("users.id")) id_of_post = db.column(db.integer,db.foreignkey("posts.id")) timestamp = db.column(db.string(60),index = true) comment = db.column(db.string(500),index = true) comment_replies = db.relationship("reply",backref = "comment_id",lazy = "dynamic")
users table:
class user(usermixin,db.model): __tablename__ = "users" id = db.column(db.integer,primary_key = true) username = db.column(db.string(60),unique = true) first_name = db.column(db.string(60),index = true) last_name = db.column(db.string(60)) password = db.column(db.string(100)) followed = db.relationship("follow",foreign_keys = [follow.followed_id],backref = db.backref("follower",lazy = "joined"),lazy = "dynamic") follower = db.relationship("follow",foreign_keys = [follow.follower_id],backref = db.backref("followed", lazy = "joined"),lazy = "dynamic") prof_pic =db.column(db.string(40)) posts = db.relationship("post",backref = "post_author", lazy = "joined") comments = db.relationship("comment", backref = "comment_author",lazy = "dynamic") replies = db.relationship("reply",backref = "reply_author",lazy = "dynamic")
so i'm trying learn web development. try , understand things decided go , and build own little social network. concepts , ideas of relational databases,flask , web dev in general
so far good. users post in descending order. latest posts seen first. people can comment on posts , it's that's breaks.
the posts ordered in correct way comments arent. commented on right post ordering based on users.id
if im user1
, comment on user2's
post comment take precedence of users2's post because im user1
.
so how query in sqlaclchemy?
in pseudocode: order posts timestamp in descending order , order comments of post timestamp in descending order
update:
this issue solved changing
posts = db.relationship("post",backref = "post_author", lazy = "joined")
to
posts = db.relationship("post",backref = "post_author", lazy = "dynamic")
Comments
Post a Comment