From eeaab86d0a801d9cb615e70362c3a7e2397a85dc Mon Sep 17 00:00:00 2001 From: Lephe Date: Sat, 24 Aug 2019 19:17:13 +0200 Subject: [PATCH] forum: improve model relationships (so that it works) --- app/models/comment.py | 9 +++-- app/models/forum.py | 6 ++-- app/models/thread.py | 12 ++++--- app/models/topic.py | 1 + ...2719_improve_relations_for_threads_and_.py | 34 +++++++++++++++++++ 5 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 migrations/versions/e3b140752719_improve_relations_for_threads_and_.py diff --git a/app/models/comment.py b/app/models/comment.py index 0e367cf..4b7fbd1 100644 --- a/app/models/comment.py +++ b/app/models/comment.py @@ -5,13 +5,18 @@ class Comment(Post): __tablename__ = 'comment' __mapper_args__ = {'polymorphic_identity': __tablename__} + # ID of the associated Post object id = db.Column(db.Integer, db.ForeignKey('post.id'), primary_key=True) # Standalone properties text = db.Column(db.UnicodeText) - # Relationships - thread_id = db.Column(db.Integer, db.ForeignKey('thread.id'), nullable=False) + # Relations + thread_id = db.Column(db.Integer, db.ForeignKey('thread.id'), + nullable=False) + thread = db.relationship('Thread', backref='comments', + foreign_keys=thread_id) + # attachement = db.relationship('Attachement', backref='comment') def __init__(self, author, text, thread): diff --git a/app/models/forum.py b/app/models/forum.py index da274a0..2d053fe 100644 --- a/app/models/forum.py +++ b/app/models/forum.py @@ -12,8 +12,10 @@ class Forum(db.Model): # Relationships parent_id = db.Column(db.Integer, db.ForeignKey('forum.id'), nullable=True) - sub_forums = db.relationship('Forum', backref='parent', lazy=True) - topics = db.relationship('Topic', backref='forum') + parent = db.relationship('Forum', backref='sub_forums', remote_side=id, + lazy=True) + + # Also [topics] which is provided by a backref from the Topic class def __init__(self, name, description, priv_prefix): self.name = name diff --git a/app/models/thread.py b/app/models/thread.py index 41b0f2c..4f6bc4c 100644 --- a/app/models/thread.py +++ b/app/models/thread.py @@ -6,21 +6,25 @@ from config import V5Config class Thread(Post): """ Some thread, such as a topic, program, tutorial """ + # Foreign Post object ID __tablename__ = 'thread' id = db.Column(db.Integer, db.ForeignKey('post.id'), primary_key=True) + # Identify threads as a type of posts using the table name, and add a + # column to further discriminate types of threads thread_type = db.Column(db.String(20)) __mapper_args__ = { 'polymorphic_identity': __tablename__, 'polymorphic_on': thread_type } - # Standalone properties + # Properties title = db.Column(db.Unicode(V5Config.THREAD_NAME_MAXLEN)) + # Also a relation [comments] populated from the Comment class. - # Relationships - top_comment = db.Column(db.Integer, db.ForeignKey('comment.id')) - comments = db.relationship('Comment', backref='thread') + # Relations + top_comment_id = db.Column(db.Integer, db.ForeignKey('comment.id')) + top_comment = db.relationship('Comment', foreign_keys=top_comment_id) def __init__(self, author, text, title): """ Create a Thread """ diff --git a/app/models/topic.py b/app/models/topic.py index b9fdedb..8e1f0fd 100644 --- a/app/models/topic.py +++ b/app/models/topic.py @@ -9,6 +9,7 @@ class Topic(Thread): # Relationships forum_id = db.Column(db.Integer, db.ForeignKey('forum.id'), nullable=False) + forum = db.relationship('Forum', backref='topics',foreign_keys=forum_id) def __init__(self, author, text, title, forum): """ Create a Topic """ diff --git a/migrations/versions/e3b140752719_improve_relations_for_threads_and_.py b/migrations/versions/e3b140752719_improve_relations_for_threads_and_.py new file mode 100644 index 0000000..00c46b7 --- /dev/null +++ b/migrations/versions/e3b140752719_improve_relations_for_threads_and_.py @@ -0,0 +1,34 @@ +"""improve relations for threads and comments + +Revision ID: e3b140752719 +Revises: 794d44c2bef8 +Create Date: 2019-08-24 19:09:46.981771 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'e3b140752719' +down_revision = '794d44c2bef8' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('thread', sa.Column('top_comment_id', sa.Integer(), nullable=True)) + op.drop_constraint('thread_top_comment_fkey', 'thread', type_='foreignkey') + op.create_foreign_key(None, 'thread', 'comment', ['top_comment_id'], ['id']) + op.drop_column('thread', 'top_comment') + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('thread', sa.Column('top_comment', sa.INTEGER(), autoincrement=False, nullable=True)) + op.drop_constraint(None, 'thread', type_='foreignkey') + op.create_foreign_key('thread_top_comment_fkey', 'thread', 'comment', ['top_comment'], ['id']) + op.drop_column('thread', 'top_comment_id') + # ### end Alembic commands ###