From df0df554021bd7fb3fe40ab0b3eee986c5467395 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Tue, 10 Jul 2018 13:54:44 +0100 Subject: [PATCH] Added unique constraint for service_id + notification_type Change updated_at to have onupdate rather than default. --- app/models.py | 6 +++++- migrations/versions/0202_service_data_retention.py | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/models.py b/app/models.py index 2f07597dc..249ecd9f6 100644 --- a/app/models.py +++ b/app/models.py @@ -1854,4 +1854,8 @@ class ServiceDataRetention(db.Model): notification_type = db.Column(notification_types, nullable=False) days_of_retention = db.Column(db.Integer, nullable=False) created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) - updated_at = db.Column(db.DateTime, nullable=True, default=datetime.datetime.utcnow) + updated_at = db.Column(db.DateTime, nullable=True, onupdate=datetime.datetime.utcnow) + + __table_args__ = ( + UniqueConstraint('service_id', 'notification_type', name='uix_service_data_retention'), + ) diff --git a/migrations/versions/0202_service_data_retention.py b/migrations/versions/0202_service_data_retention.py index a85ead60f..438c78fce 100644 --- a/migrations/versions/0202_service_data_retention.py +++ b/migrations/versions/0202_service_data_retention.py @@ -17,14 +17,14 @@ def upgrade(): op.create_table('service_data_retention', sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=False), - sa.Column('notification_type', - postgresql.ENUM(name='notification_type', create_type=False), + sa.Column('notification_type', postgresql.ENUM(name='notification_type', create_type=False), nullable=False), sa.Column('days_of_retention', sa.Integer(), nullable=False), sa.Column('created_at', sa.DateTime(), nullable=False), sa.Column('updated_at', sa.DateTime(), nullable=True), sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), - sa.PrimaryKeyConstraint('id') + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('service_id', 'notification_type', name='uix_service_data_retention') ) op.create_index(op.f('ix_service_data_retention_service_id'), 'service_data_retention', ['service_id'], unique=False)