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}")