From f6c740a586001d06a4341774daad4ee7e30c6afb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simon=20K=C3=BCnzel?= <simonk@fsmpi.rwth-aachen.de>
Date: Sun, 9 Feb 2025 23:56:10 +0100
Subject: [PATCH] Add constraints for featured values

---
 src/videoag_common/objects/site.py | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/src/videoag_common/objects/site.py b/src/videoag_common/objects/site.py
index 96ae6b9..5adbfd1 100644
--- a/src/videoag_common/objects/site.py
+++ b/src/videoag_common/objects/site.py
@@ -90,6 +90,21 @@ class Featured(DeletableApiObject, VisibilityApiObject, Base):
         "with_polymorphic": "*",  # Always load all attributes for all types,
         "polymorphic_identity": FeaturedType.PLAIN
     }
+    __table_args__ = (
+        CheckConstraint(
+            f"type NOT IN ('image') OR image_url IS NOT NULL",
+            name="check_image_set"
+        ),
+        CheckConstraint(
+            f"type NOT IN ('course') OR course_id IS NOT NULL",
+            name="check_course_id_set"
+        ),
+        CheckConstraint(
+            f"type NOT IN ('lecture') OR lecture_id IS NOT NULL",
+            name="check_lecture_id_set"
+        ),
+    )
+    
     title: Mapped[str] = api_mapped(
         mapped_column(Text(collation=STRING_COLLATION), nullable=False, default=""),
         ApiStringField(
-- 
GitLab