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.
This commit is contained in:
Yuyao Huang 2026-05-09 13:05:53 +08:00
parent 6a2e0537ea
commit 427f62acca
2 changed files with 2 additions and 1 deletions

View File

@ -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:

View File

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