From d28390d79249c2080443f45fb04c12e6f7f5465e Mon Sep 17 00:00:00 2001 From: kolaente Date: Mon, 19 Jul 2021 23:40:18 +0200 Subject: [PATCH] Fix task relations not getting properly cleaned up when deleting them --- pkg/models/task_relation.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/pkg/models/task_relation.go b/pkg/models/task_relation.go index 272972a6..4be80a4c 100644 --- a/pkg/models/task_relation.go +++ b/pkg/models/task_relation.go @@ -19,6 +19,7 @@ package models import ( "time" + "xorm.io/builder" "xorm.io/xorm" "code.vikunja.io/api/pkg/user" @@ -208,9 +209,23 @@ func (rel *TaskRelation) Create(s *xorm.Session, a web.Auth) error { // @Failure 500 {object} models.Message "Internal error" // @Router /tasks/{taskID}/relations/{relationKind}/{otherTaskId} [delete] func (rel *TaskRelation) Delete(s *xorm.Session, a web.Auth) error { + + cond := builder.Or( + builder.And( + builder.Eq{"task_id": rel.TaskID}, + builder.Eq{"other_task_id": rel.OtherTaskID}, + builder.Eq{"relation_kind": rel.RelationKind}, + ), + builder.And( + builder.Eq{"task_id": rel.OtherTaskID}, + builder.Eq{"other_task_id": rel.TaskID}, + builder.Eq{"relation_kind": rel.RelationKind}, + ), + ) + // Check if the relation exists exists, err := s. - Where("task_id = ? AND other_task_id = ? AND relation_kind = ?", rel.TaskID, rel.OtherTaskID, rel.RelationKind). + Where(cond). Exist(&TaskRelation{}) if err != nil { return err @@ -223,6 +238,8 @@ func (rel *TaskRelation) Delete(s *xorm.Session, a web.Auth) error { } } - _, err = s.Delete(rel) + _, err = s. + Where(cond). + Delete(&TaskRelation{}) return err }