From 54abe8ac3b43a96587577a9024f904eda60d108f Mon Sep 17 00:00:00 2001 From: bruno Date: Sat, 11 Jul 2026 07:38:15 -0400 Subject: [PATCH] fix: FOREIGN KEY constraint failed on POST /api/workspaces Bug: create_workspace in dashboard.py set uid=1 when session was invalid, but user 1 might not exist in the users table, causing sqlite3.IntegrityError: FOREIGN KEY constraint failed (HTTP 500). Fix: ensure user row exists (INSERT OR IGNORE) before inserting workspace with FK reference, matching the pattern in workspace.py. Also: add name validation, use INSERT OR IGNORE for members. --- app/routers/dashboard.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/routers/dashboard.py b/app/routers/dashboard.py index 585b77e..830e987 100644 --- a/app/routers/dashboard.py +++ b/app/routers/dashboard.py @@ -659,18 +659,27 @@ async def create_workspace(request: Request): import json body = await request.json() name = body.get("name", "New Workspace").strip() + if not name: + return {"error": "Name required"} user = SessionManager.decode_session(request.cookies.get("flowdeck_session", "")) uid = user["id"] if user and user.get("id") else 1 with get_conn() as conn: + # Ensure user exists (FK constraint) + uid_ok = conn.execute("SELECT id FROM users WHERE id=?", (uid,)).fetchone() + if not uid_ok: + conn.execute( + "INSERT OR IGNORE INTO users (id, login, full_name, is_admin) VALUES (?, ?, ?, 1)", + (uid, user.get("login", "admin") if user else "admin", + user.get("full_name", "Admin") if user else "Admin"), + ) cursor = conn.execute( "INSERT INTO workspaces (name, owner_id) VALUES (?, ?)", (name, uid), ) - conn.commit() ws_id = cursor.lastrowid # Add owner as member conn.execute( - "INSERT INTO workspace_members (workspace_id, user_id, role) VALUES (?, ?, 'owner')", + "INSERT OR IGNORE INTO workspace_members (workspace_id, user_id, role) VALUES (?, ?, 'owner')", (ws_id, uid), ) conn.commit()