forum: improve model relationships (so that it works)

This commit is contained in:
Lephe 2019-08-24 19:17:13 +02:00
parent 11b19af199
commit eeaab86d0a
5 changed files with 54 additions and 8 deletions

View File

@ -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):

View File

@ -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

View File

@ -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 """

View File

@ -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 """

View File

@ -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 ###