Fix type annotation for location_id; change from Optional[str] to Optional[int] for correct database mapping

This commit is contained in:
Yaro Kasear 2025-06-23 15:49:31 -05:00
parent 8162038f40
commit 04d262ae26
2 changed files with 5 additions and 4 deletions

View file

@ -17,7 +17,7 @@ class User(db.Model):
active: Mapped[Optional[bool]] = mapped_column("Active", Boolean, server_default=text('((0))')) active: Mapped[Optional[bool]] = mapped_column("Active", Boolean, server_default=text('((0))'))
last_name: Mapped[Optional[str]] = mapped_column("Last", Unicode(255), nullable=True) last_name: Mapped[Optional[str]] = mapped_column("Last", Unicode(255), nullable=True)
first_name: Mapped[Optional[str]] = mapped_column("First", 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_id: Mapped[Optional[int]] = mapped_column("Supervisor", Integer, ForeignKey("Users.ID"))
supervisor: Mapped[Optional['User']] = relationship('User', remote_side='User.id', back_populates='subordinates') supervisor: Mapped[Optional['User']] = relationship('User', remote_side='User.id', back_populates='subordinates')

View file

@ -407,22 +407,23 @@ def settings():
if mapper is not None: if mapper is not None:
db.session.flush() db.session.flush()
mapper[clean] = new_obj.id mapper[clean] = new_obj.id
def resolve_id(raw_id, fallback_list, id_map, label): def resolve_id(raw_id, fallback_list, id_map, label):
try: try:
resolved_id = int(raw_id) resolved_id = int(raw_id)
except (TypeError, ValueError): except (TypeError, ValueError):
raise ValueError(f"{label.title()} ID was not a valid integer: {raw_id}") raise ValueError(f"{label.title()} ID was not a valid integer: {raw_id}")
if resolved_id >= 0: if resolved_id >= 0:
# Try to validate this ID by checking if it appears in the map values # Try to validate this ID by checking if it appears in the map values
if resolved_id in id_map.values(): if resolved_id in id_map.values():
return resolved_id return resolved_id
else: else:
raise ValueError(f"{label.title()} ID {resolved_id} not found in known {label}s.") 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 # It's a negative ID = created on frontend. Resolve from fallback list
index = abs(resolved_id + 1) index = abs(resolved_id + 1)
entry = None # Ensure entry is always defined
try: try:
entry = fallback_list[index] entry = fallback_list[index]
key = entry["name"] if isinstance(entry, dict) else str(entry).strip() key = entry["name"] if isinstance(entry, dict) else str(entry).strip()