Skip to content

Commit 277aaaf

Browse files
Enforce single result per modelinput
In Acidwatch we allow for a simulation consisting of multiple model inputs (chaining models), and for each modelinput there corresponds a modelresult. This was added as part of the orm, but we should enforce it also on the db level.
1 parent d294eae commit 277aaaf

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""add unique constraint on results.model_input_id
2+
3+
Revision ID: a3f8b2c91d40
4+
Revises: 7c2e1f4b8a90
5+
Create Date: 2026-06-16 00:00:00.000000
6+
7+
Enforces the intended one-to-one relationship between model_inputs and
8+
results at the database level. The ORM already declares this as a singular
9+
``Mapped[ModelResult | None]`` relationship, but previously no unique
10+
constraint prevented duplicate results for a single model_input.
11+
"""
12+
13+
from typing import Sequence, Union
14+
15+
from alembic import op
16+
17+
18+
revision: str = "a3f8b2c91d40"
19+
down_revision: Union[str, Sequence[str], None] = "7c2e1f4b8a90"
20+
branch_labels: Union[str, Sequence[str], None] = None
21+
depends_on: Union[str, Sequence[str], None] = None
22+
23+
24+
def upgrade() -> None:
25+
op.create_unique_constraint(
26+
"uq_results_model_input_id", "results", ["model_input_id"]
27+
)
28+
29+
30+
def downgrade() -> None:
31+
op.drop_constraint("uq_results_model_input_id", "results", type_="unique")

backend/src/acidwatch_api/database.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ class ModelInput(Base):
6767
class ModelResult(Base):
6868
__tablename__ = "results"
6969

70-
model_input_id: Mapped[UUID] = mapped_column(ForeignKey("model_inputs.id"))
70+
model_input_id: Mapped[UUID] = mapped_column(
71+
ForeignKey("model_inputs.id"), unique=True
72+
)
7173
concentrations: Mapped[dict[str, float]] = mapped_column(JSON)
7274
panels: Mapped[list[Any]] = mapped_column(JSON)
7375
error: Mapped[str | None] = mapped_column()

0 commit comments

Comments
 (0)