fix: use column names for COUNT queries with RealDictCursor
All checks were successful
Deploy Development / deploy (push) Successful in 58s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 12s

RealDictCursor returns dicts, not tuples. Cannot use [0] for index access.
Changed all COUNT(*) to COUNT(*) as count and access via ['count'].

Fixes: KeyError: 0 on cur.fetchone()[0]

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Lars 2026-03-18 12:47:01 +01:00
parent 9fbedb6c4b
commit 79a951ce92

View File

@ -198,8 +198,8 @@ def update_profile(pid: str, p: ProfileUpdate, session=Depends(require_auth)):
def delete_profile(pid: str, session=Depends(require_auth)):
with get_db() as conn:
cur = get_cursor(conn)
cur.execute("SELECT COUNT(*) FROM profiles")
count = cur.fetchone()[0]
cur.execute("SELECT COUNT(*) as count FROM profiles")
count = cur.fetchone()['count']
if count <= 1: raise HTTPException(400, "Letztes Profil kann nicht gelöscht werden")
for table in ['weight_log','circumference_log','caliper_log','nutrition_log','activity_log','ai_insights']:
cur.execute(f"DELETE FROM {table} WHERE profile_id=%s", (pid,))
@ -623,16 +623,16 @@ def get_stats(x_profile_id: Optional[str]=Header(default=None), session: dict=De
pid = get_pid(x_profile_id)
with get_db() as conn:
cur = get_cursor(conn)
cur.execute("SELECT COUNT(*) FROM weight_log WHERE profile_id=%s",(pid,))
weight_count = cur.fetchone()[0]
cur.execute("SELECT COUNT(*) FROM circumference_log WHERE profile_id=%s",(pid,))
circ_count = cur.fetchone()[0]
cur.execute("SELECT COUNT(*) FROM caliper_log WHERE profile_id=%s",(pid,))
caliper_count = cur.fetchone()[0]
cur.execute("SELECT COUNT(*) FROM nutrition_log WHERE profile_id=%s",(pid,))
nutrition_count = cur.fetchone()[0]
cur.execute("SELECT COUNT(*) FROM activity_log WHERE profile_id=%s",(pid,))
activity_count = cur.fetchone()[0]
cur.execute("SELECT COUNT(*) as count FROM weight_log WHERE profile_id=%s",(pid,))
weight_count = cur.fetchone()['count']
cur.execute("SELECT COUNT(*) as count FROM circumference_log WHERE profile_id=%s",(pid,))
circ_count = cur.fetchone()['count']
cur.execute("SELECT COUNT(*) as count FROM caliper_log WHERE profile_id=%s",(pid,))
caliper_count = cur.fetchone()['count']
cur.execute("SELECT COUNT(*) as count FROM nutrition_log WHERE profile_id=%s",(pid,))
nutrition_count = cur.fetchone()['count']
cur.execute("SELECT COUNT(*) as count FROM activity_log WHERE profile_id=%s",(pid,))
activity_count = cur.fetchone()['count']
return {
"weight_count": weight_count,
"circ_count": circ_count,
@ -1204,10 +1204,10 @@ def admin_list_profiles(session: dict=Depends(require_admin)):
for p in profs:
pid = p['id']
cur.execute("SELECT COUNT(*) FROM weight_log WHERE profile_id=%s", (pid,))
p['weight_count'] = cur.fetchone()[0]
cur.execute("SELECT COUNT(*) FROM ai_insights WHERE profile_id=%s", (pid,))
p['ai_insights_count'] = cur.fetchone()[0]
cur.execute("SELECT COUNT(*) as count FROM weight_log WHERE profile_id=%s", (pid,))
p['weight_count'] = cur.fetchone()['count']
cur.execute("SELECT COUNT(*) as count FROM ai_insights WHERE profile_id=%s", (pid,))
p['ai_insights_count'] = cur.fetchone()['count']
today = datetime.now().date().isoformat()
cur.execute("SELECT call_count FROM ai_usage WHERE profile_id=%s AND date=%s", (pid, today))