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:
parent
6a2e0537ea
commit
427f62acca
@ -190,6 +190,7 @@ def update_task(task_id, **kwargs):
|
|||||||
def delete_task(task_id):
|
def delete_task(task_id):
|
||||||
conn = get_connection()
|
conn = get_connection()
|
||||||
try:
|
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.execute("DELETE FROM tasks WHERE id = ?", (task_id,))
|
||||||
conn.commit()
|
conn.commit()
|
||||||
finally:
|
finally:
|
||||||
|
|||||||
@ -22,7 +22,7 @@ CREATE TABLE IF NOT EXISTS goals (
|
|||||||
activated INTEGER NOT NULL DEFAULT 1,
|
activated INTEGER NOT NULL DEFAULT 1,
|
||||||
selected_task_id INTEGER,
|
selected_task_id INTEGER,
|
||||||
FOREIGN KEY (user_id) REFERENCES users(id),
|
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
|
||||||
)
|
)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user