diff --git a/app/models.py b/app/models.py index 7b7e012aa..3032a10ad 100644 --- a/app/models.py +++ b/app/models.py @@ -1181,8 +1181,7 @@ class NotificationHistory(db.Model, HistoryModel): job_row_number = db.Column(db.Integer, nullable=True) service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, unique=False) service = db.relationship('Service') - template_id = db.Column(UUID(as_uuid=True), db.ForeignKey('templates.id'), index=True, unique=False) - template = db.relationship('Template') + template_id = db.Column(UUID(as_uuid=True), index=True, unique=False) template_version = db.Column(db.Integer, nullable=False) api_key_id = db.Column(UUID(as_uuid=True), db.ForeignKey('api_keys.id'), index=True, unique=False) api_key = db.relationship('ApiKey') @@ -1212,6 +1211,14 @@ class NotificationHistory(db.Model, HistoryModel): created_by = db.relationship('User') created_by_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), nullable=True) + __table_args__ = ( + db.ForeignKeyConstraint( + ['template_id', 'template_version'], + ['templates_history.id', 'templates_history.version'], + ), + {} + ) + @classmethod def from_original(cls, notification): history = super().from_original(notification) diff --git a/tests/app/conftest.py b/tests/app/conftest.py index f2c2cd41f..cecfb5a27 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -679,7 +679,7 @@ def sample_notification_history( notification_history = NotificationHistory( id=uuid.uuid4(), service=sample_template.service, - template=sample_template, + template_id=sample_template.id, template_version=sample_template.version, status=status, created_at=created_at, diff --git a/tests/app/dao/test_provider_statistics_dao.py b/tests/app/dao/test_provider_statistics_dao.py index 54e429b0e..f23550b6b 100644 --- a/tests/app/dao/test_provider_statistics_dao.py +++ b/tests/app/dao/test_provider_statistics_dao.py @@ -73,7 +73,7 @@ def noti_hist(notify_db, template, status='delivered', billable_units=None, key_ notification_history = NotificationHistory( id=uuid.uuid4(), service=template.service, - template=template, + template_id=template.id, template_version=template.version, status=status, created_at=datetime.utcnow(),