fix: use column names for COUNT queries with RealDictCursor
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:
parent
9fbedb6c4b
commit
79a951ce92
|
|
@ -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))
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user