diff --git a/app/database/models.py b/app/database/models.py index fed3233a..70c9874a 100644 --- a/app/database/models.py +++ b/app/database/models.py @@ -560,12 +560,15 @@ class SentNotification(Base): __tablename__ = "sent_notifications" id = Column(Integer, primary_key=True, index=True) - user_id = Column(Integer, ForeignKey("users.id"), nullable=False) - subscription_id = Column(Integer, ForeignKey("subscriptions.id"), nullable=False) + user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False) + subscription_id = Column(Integer, ForeignKey("subscriptions.id", ondelete="CASCADE"), nullable=False) notification_type = Column(String(50), nullable=False) days_before = Column(Integer, nullable=True) created_at = Column(DateTime, default=func.now()) + user = relationship("User", backref="sent_notifications") + subscription = relationship("Subscription", backref="sent_notifications") + class BroadcastHistory(Base): __tablename__ = "broadcast_history" diff --git a/migrations/alembic/versions/cbd1be472f3d_add_cascade_to_sent_notifications.py b/migrations/alembic/versions/cbd1be472f3d_add_cascade_to_sent_notifications.py new file mode 100644 index 00000000..08ae5543 --- /dev/null +++ b/migrations/alembic/versions/cbd1be472f3d_add_cascade_to_sent_notifications.py @@ -0,0 +1,24 @@ +"""add cascade delete to sent notifications""" + +from typing import Sequence, Union +from alembic import op +import sqlalchemy as sa + +revision: str = 'cbd1be472f3d' +down_revision: Union[str, None] = '8fd1e338eb45' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + op.drop_constraint('sent_notifications_user_id_fkey', 'sent_notifications', type_='foreignkey') + op.drop_constraint('sent_notifications_subscription_id_fkey', 'sent_notifications', type_='foreignkey') + op.create_foreign_key('fk_sent_notifications_user_id_users', 'sent_notifications', 'users', ['user_id'], ['id'], ondelete='CASCADE') + op.create_foreign_key('fk_sent_notifications_subscription_id_subscriptions', 'sent_notifications', 'subscriptions', ['subscription_id'], ['id'], ondelete='CASCADE') + + +def downgrade() -> None: + op.drop_constraint('fk_sent_notifications_user_id_users', 'sent_notifications', type_='foreignkey') + op.drop_constraint('fk_sent_notifications_subscription_id_subscriptions', 'sent_notifications', type_='foreignkey') + op.create_foreign_key('sent_notifications_user_id_fkey', 'sent_notifications', 'users', ['user_id'], ['id']) + op.create_foreign_key('sent_notifications_subscription_id_fkey', 'sent_notifications', 'subscriptions', ['subscription_id'], ['id'])