diff --git a/tildes/alembic/versions/9fc0033a2b61_topic_add_schedule_id_column.py b/tildes/alembic/versions/9fc0033a2b61_topic_add_schedule_id_column.py new file mode 100644 index 0000000..35c4636 --- /dev/null +++ b/tildes/alembic/versions/9fc0033a2b61_topic_add_schedule_id_column.py @@ -0,0 +1,38 @@ +"""Topic: add schedule_id column + +Revision ID: 9fc0033a2b61 +Revises: 8326f8cc5ddd +Create Date: 2019-10-12 01:51:26.045258 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "9fc0033a2b61" +down_revision = "8326f8cc5ddd" +branch_labels = None +depends_on = None + + +def upgrade(): + op.add_column("topics", sa.Column("schedule_id", sa.Integer(), nullable=True)) + op.create_index( + op.f("ix_topics_schedule_id"), "topics", ["schedule_id"], unique=False + ) + op.create_foreign_key( + op.f("fk_topics_schedule_id_topic_schedule"), + "topics", + "topic_schedule", + ["schedule_id"], + ["schedule_id"], + ) + + +def downgrade(): + op.drop_constraint( + op.f("fk_topics_schedule_id_topic_schedule"), "topics", type_="foreignkey" + ) + op.drop_index(op.f("ix_topics_schedule_id"), table_name="topics") + op.drop_column("topics", "schedule_id") diff --git a/tildes/tildes/models/topic/topic.py b/tildes/tildes/models/topic/topic.py index 124135d..45d5864 100644 --- a/tildes/tildes/models/topic/topic.py +++ b/tildes/tildes/models/topic/topic.py @@ -78,6 +78,9 @@ class Topic(DatabaseModel): user_id: int = Column( Integer, ForeignKey("users.user_id"), nullable=False, index=True ) + schedule_id: int = Column( + Integer, ForeignKey("topic_schedule.schedule_id"), index=True + ) created_time: datetime = Column( TIMESTAMP(timezone=True), nullable=False, diff --git a/tildes/tildes/models/topic/topic_schedule.py b/tildes/tildes/models/topic/topic_schedule.py index 979f3b1..174e5da 100644 --- a/tildes/tildes/models/topic/topic_schedule.py +++ b/tildes/tildes/models/topic/topic_schedule.py @@ -8,7 +8,7 @@ from typing import List, Optional from dateutil.rrule import rrule from sqlalchemy import CheckConstraint, Column, ForeignKey, Integer, Text, TIMESTAMP -from sqlalchemy.orm import relationship +from sqlalchemy.orm import backref, relationship from sqlalchemy.orm.session import Session from sqlalchemy.sql.expression import text @@ -48,6 +48,8 @@ class TopicSchedule(DatabaseModel): group: Group = relationship("Group", innerjoin=True) user: Optional[User] = relationship("User") + topics: List[Topic] = relationship(Topic, backref=backref("schedule")) + def __init__( self, group: Group, @@ -82,6 +84,7 @@ class TopicSchedule(DatabaseModel): topic = Topic.create_text_topic(self.group, user, self.title, self.markdown) topic.tags = self.tags + topic.schedule = self return topic