From 427f62acca946cc0f12cd167028c580a920db315 Mon Sep 17 00:00:00 2001 From: Yuyao Huang Date: Sat, 9 May 2026 13:05:53 +0800 Subject: [PATCH] fix: handle foreign key constraint when deleting tasks with selected_task_id refs Goals table has selected_task_id referencing tasks(id) without ON DELETE SET NULL. When deleting a task, first clear any selected_task_id references in goals to prevent FK violation. Also update schema for future databases. --- database.py | 1 + schema.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/database.py b/database.py index b6a93e3..13db531 100644 --- a/database.py +++ b/database.py @@ -190,6 +190,7 @@ def update_task(task_id, **kwargs): def delete_task(task_id): conn = get_connection() try: + conn.execute('UPDATE goals SET "selected_task_id" = NULL WHERE "selected_task_id" = ?', (task_id,)) conn.execute("DELETE FROM tasks WHERE id = ?", (task_id,)) conn.commit() finally: diff --git a/schema.py b/schema.py index a9e1f02..53c092c 100644 --- a/schema.py +++ b/schema.py @@ -22,7 +22,7 @@ CREATE TABLE IF NOT EXISTS goals ( activated INTEGER NOT NULL DEFAULT 1, selected_task_id INTEGER, FOREIGN KEY (user_id) REFERENCES users(id), - FOREIGN KEY (selected_task_id) REFERENCES tasks(id) + FOREIGN KEY (selected_task_id) REFERENCES tasks(id) ON DELETE SET NULL ) """