From 04d262ae26995741edc58524dc18857b7659ce4d Mon Sep 17 00:00:00 2001 From: Yaro Kasear Date: Mon, 23 Jun 2025 15:49:31 -0500 Subject: [PATCH] Fix type annotation for location_id; change from Optional[str] to Optional[int] for correct database mapping --- models/users.py | 2 +- routes.py | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/models/users.py b/models/users.py index c667da0..5d95df0 100644 --- a/models/users.py +++ b/models/users.py @@ -17,7 +17,7 @@ class User(db.Model): active: Mapped[Optional[bool]] = mapped_column("Active", Boolean, server_default=text('((0))')) last_name: Mapped[Optional[str]] = mapped_column("Last", Unicode(255), nullable=True) first_name: Mapped[Optional[str]] = mapped_column("First", Unicode(255), nullable=True) - location_id: Mapped[Optional[str]] = mapped_column(ForeignKey("Rooms.ID"), nullable=True) + location_id: Mapped[Optional[int]] = mapped_column(ForeignKey("Rooms.ID"), nullable=True) supervisor_id: Mapped[Optional[int]] = mapped_column("Supervisor", Integer, ForeignKey("Users.ID")) supervisor: Mapped[Optional['User']] = relationship('User', remote_side='User.id', back_populates='subordinates') diff --git a/routes.py b/routes.py index 621a183..2c3be9f 100644 --- a/routes.py +++ b/routes.py @@ -407,22 +407,23 @@ def settings(): if mapper is not None: db.session.flush() mapper[clean] = new_obj.id - + def resolve_id(raw_id, fallback_list, id_map, label): try: resolved_id = int(raw_id) except (TypeError, ValueError): raise ValueError(f"{label.title()} ID was not a valid integer: {raw_id}") - + if resolved_id >= 0: # Try to validate this ID by checking if it appears in the map values if resolved_id in id_map.values(): return resolved_id else: raise ValueError(f"{label.title()} ID {resolved_id} not found in known {label}s.") - + # It's a negative ID = created on frontend. Resolve from fallback list index = abs(resolved_id + 1) + entry = None # Ensure entry is always defined try: entry = fallback_list[index] key = entry["name"] if isinstance(entry, dict) else str(entry).strip()