From ce913e38ada2b6fbd5421d75b75b0a5cf621f510 Mon Sep 17 00:00:00 2001 From: Kunal Baviskar Date: Mon, 9 Jun 2025 14:44:31 +0530 Subject: [PATCH] Trigger table creation task on startup event, autocommit = True Trigger table creation task on startup event instead of get API to avoid first time manual hit. Use autocommit = True. Noticed issues with conn.commit() while creating DB/table from backend. --- app.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index ed139e2..d3a522b 100644 --- a/app.py +++ b/app.py @@ -28,10 +28,10 @@ class Task(BaseModel): description: str # Create a table for tasks (You can run this once outside of the app) -@app.get("/api") +@app.on_event("startup") def create_tasks_table(): try: - conn = pyodbc.connect(connection_string) + conn = pyodbc.connect(connection_string, autocommit=True) cursor = conn.cursor() cursor.execute(""" CREATE TABLE Tasks ( @@ -39,8 +39,7 @@ def create_tasks_table(): Title varchar(255), Description text ); - """) - conn.commit() + """) except Exception as e: print(e) return "Table Created... Tasks API Ready"