diff --git a/app/models/attachment.py b/app/models/attachment.py index b710298..a822325 100644 --- a/app/models/attachment.py +++ b/app/models/attachment.py @@ -18,7 +18,8 @@ class Attachment(db.Model): # The comment linked with comment_id = db.Column(db.Integer, db.ForeignKey('comment.id'), nullable=False, index=True) - comment = db.relationship('Comment', backref=backref('attachments')) + comment = db.relationship('Comment', back_populates='attachments', + foreign_keys=comment_id) # The size of the file size = db.Column(db.Integer) diff --git a/app/models/comment.py b/app/models/comment.py index f5cb42a..a7386db 100644 --- a/app/models/comment.py +++ b/app/models/comment.py @@ -20,7 +20,8 @@ class Comment(Post): backref=backref('comments', lazy='dynamic'), foreign_keys=thread_id) - # attachments (relation from Attachment) + attachments = db.relationship('Attachment', back_populates='comment', + lazy='joined') @property def is_top_comment(self): diff --git a/app/models/priv.py b/app/models/priv.py index 2977227..32cecbb 100644 --- a/app/models/priv.py +++ b/app/models/priv.py @@ -17,7 +17,7 @@ class SpecialPrivilege(db.Model): # Member that is granted the privilege member_id = db.Column(db.Integer, db.ForeignKey('member.id'), index=True) - member = db.relationship('Member', backref="special_privs", + member = db.relationship('Member', back_populates="special_privs", foreign_keys=member_id) # Privilege name priv = db.Column(db.String(64)) @@ -45,10 +45,9 @@ class Group(db.Model): description = db.Column(db.UnicodeText) # List of members (lambda delays evaluation) members = db.relationship('Member', secondary=lambda: GroupMember, - back_populates='groups') - - # Other fields populated automatically through relations: - # list of privileges + back_populates='groups', lazy='joined') + # List of privileges + privileges = db.relationship('GroupPrivilege', back_populates='group') def __init__(self, name, css, descr): self.name = name @@ -87,7 +86,7 @@ class GroupPrivilege(db.Model): id = db.Column(db.Integer, primary_key=True) group_id = db.Column(db.Integer, db.ForeignKey('group.id')) - group = db.relationship('Group', backref='privileges', + group = db.relationship('Group', back_populates='privileges', foreign_keys=group_id) priv = db.Column(db.String(64)) diff --git a/app/models/user.py b/app/models/user.py index 7a50a24..afbab8c 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -92,7 +92,7 @@ class Member(User): # Groups and related privileges groups = db.relationship('Group', secondary=GroupMember, - back_populates='members') + back_populates='members', lazy='joined') # Personal information, all optional bio = db.Column(db.UnicodeText) @@ -112,6 +112,15 @@ class Member(User): trophies = db.relationship('Trophy', secondary=TrophyMember, back_populates='owners') + # Specially-offered privileges (use self.special_privileges()) + special_privs = db.relationship('SpecialPrivilege', + back_populates='member', lazy='joined') + + + # Other fields populated automatically through relations: + # List of unseen notifications (of type Notification) + # Polls created by the member (of class Poll) + # Access to polymorphic posts # TODO: Check that the query uses the double index on Post.{author_id,type} def comments(self): @@ -138,11 +147,6 @@ class Member(User): return int(level), int(level * 100) % 100 - # Other fields populated automatically through relations: - # List of special privileges - # List of unseen notifications (of type Notification) - # Polls created by the member (of class Poll) - def __init__(self, name, email, password): """Register a new user.""" self.name = name