From c8e005dd0739f4dbc97089b9b45bc795850fc56e Mon Sep 17 00:00:00 2001 From: Christian Date: Fri, 19 Dec 2025 16:30:03 +0100 Subject: [PATCH] Fix: Only fetchall() when query has RETURNING clause or is SELECT --- app/core/database.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/app/core/database.py b/app/core/database.py index f29e4d2..4dac789 100644 --- a/app/core/database.py +++ b/app/core/database.py @@ -64,12 +64,18 @@ def execute_query(query: str, params: tuple = None, fetch: bool = True): # Auto-detect write operations and commit query_upper = query.strip().upper() - if query_upper.startswith(('INSERT', 'UPDATE', 'DELETE')): + is_write = query_upper.startswith(('INSERT', 'UPDATE', 'DELETE')) + + if is_write: conn.commit() - if fetch: + # Only fetch if there are results to fetch + # (SELECT queries or INSERT/UPDATE/DELETE with RETURNING clause) + if fetch and (not is_write or 'RETURNING' in query_upper): return cursor.fetchall() - return cursor.rowcount + elif is_write: + return cursor.rowcount + return [] except Exception as e: conn.rollback() logger.error(f"Query error: {e}")