Summary
Five bugs found while reading the backend services that cause unhandled crashes or silent data corruption.
Bug 1 — services/code_generation.py: AttributeError when model not found
File: tensormap-backend/app/services/code_generation.py
generate_code() accesses model_configs.file_id without checking if model_configs is None first. If the model name doesn't exist in the DB, this raises AttributeError and returns 500 instead of a clean 404.
# Current — crashes if model not found
model_configs = db.exec(...).first()
file = db.exec(select(DataFile).where(DataFile.id == model_configs.file_id)).first() # AttributeError
# Fix
if model_configs is None:
raise ValueError(f"Model '{model_name}' not found in database")
Bug 2 — services/code_generation.py: KeyError for unknown problem type
File: tensormap-backend/app/services/code_generation.py
_map_template() uses options[problem_type_id] (dict key access) instead of options.get(problem_type_id). Any unrecognized model_type raises KeyError → 500 with no useful message.
# Current — KeyError on unknown type
return options[problem_type_id]
# Fix
return options.get(problem_type_id) # returns None, caller raises ValueError with clear message
Bug 3 — services/model_run.py: AttributeError when model not found
File: tensormap-backend/app/services/model_run.py
Same pattern as Bug 1. _run() does not check if model_configs is None before accessing .model_type, crashing with AttributeError.
# Current — crashes if model not found
model_configs = db.exec(...).first()
if model_configs.model_type == ProblemType.IMAGE_CLASSIFICATION: # AttributeError
Bug 4 — services/model_run.py: KeyError / crash when target_field is None or missing from CSV
File: tensormap-backend/app/services/model_run.py
features.drop(model_configs.target_field, axis=1) crashes with KeyError if:
target_field is None (user hasn't configured training yet)
target_field doesn't exist as a column in the CSV
The error message is unhelpful and the root cause is unclear to the user.
Bug 5 — services/data_upload.py: Uploading a duplicate filename silently overwrites the file on disk
File: tensormap-backend/app/services/data_upload.py
If a user uploads a file with the same name as an existing file, the new file overwrites the old one on disk, but a new DB record is created. The old DB record now points to a file with different content — causing silent data corruption.
Fix: Check for existing (file_name, file_type) in DB before saving. Return 409 Conflict if duplicate found.
Steps to Reproduce
- Bug 1 & 2: Call
POST /model/code with a model_name that doesn't exist in the DB
- Bug 3: Call
POST /model/run with a model_name that doesn't exist in the DB
- Bug 4: Call
POST /model/run on a model where target_field is None or was deleted from the CSV
- Bug 5: Upload the same CSV file twice
Expected Behavior
Clean error responses (400/404/409) with descriptive messages instead of unhandled 500 crashes or silent data corruption.
Summary
Five bugs found while reading the backend services that cause unhandled crashes or silent data corruption.
Bug 1 —
services/code_generation.py: AttributeError when model not foundFile:
tensormap-backend/app/services/code_generation.pygenerate_code()accessesmodel_configs.file_idwithout checking ifmodel_configsisNonefirst. If the model name doesn't exist in the DB, this raisesAttributeErrorand returns 500 instead of a clean 404.Bug 2 —
services/code_generation.py: KeyError for unknown problem typeFile:
tensormap-backend/app/services/code_generation.py_map_template()usesoptions[problem_type_id](dict key access) instead ofoptions.get(problem_type_id). Any unrecognizedmodel_typeraisesKeyError→ 500 with no useful message.Bug 3 —
services/model_run.py: AttributeError when model not foundFile:
tensormap-backend/app/services/model_run.pySame pattern as Bug 1.
_run()does not check ifmodel_configsisNonebefore accessing.model_type, crashing withAttributeError.Bug 4 —
services/model_run.py: KeyError / crash whentarget_fieldis None or missing from CSVFile:
tensormap-backend/app/services/model_run.pyfeatures.drop(model_configs.target_field, axis=1)crashes withKeyErrorif:target_fieldisNone(user hasn't configured training yet)target_fielddoesn't exist as a column in the CSVThe error message is unhelpful and the root cause is unclear to the user.
Bug 5 —
services/data_upload.py: Uploading a duplicate filename silently overwrites the file on diskFile:
tensormap-backend/app/services/data_upload.pyIf a user uploads a file with the same name as an existing file, the new file overwrites the old one on disk, but a new DB record is created. The old DB record now points to a file with different content — causing silent data corruption.
Fix: Check for existing
(file_name, file_type)in DB before saving. Return409 Conflictif duplicate found.Steps to Reproduce
POST /model/codewith amodel_namethat doesn't exist in the DBPOST /model/runwith amodel_namethat doesn't exist in the DBPOST /model/runon a model wheretarget_fieldisNoneor was deleted from the CSVExpected Behavior
Clean error responses (400/404/409) with descriptive messages instead of unhandled 500 crashes or silent data corruption.