]> Untitled Git - lemmy.git/blobdiff - migrations/2020-12-10-152350_create_post_aggregates/up.sql
Adding tests for post aggregates.
[lemmy.git] / migrations / 2020-12-10-152350_create_post_aggregates / up.sql
index f9321bebbc88165bb53444649d08e7e0b4d5bdd7..b3dc6278d241a8c62b503ef082c92fb898bc8e6a 100644 (file)
@@ -66,9 +66,12 @@ begin
     newest_comment_time = NEW.published
     where pa.post_id = NEW.post_id;
   ELSIF (TG_OP = 'DELETE') THEN
+    -- Join to post because that post may not exist anymore
     update post_aggregates pa
     set comments = comments - 1
-    where pa.post_id = OLD.post_id;
+    from post p
+    where pa.post_id = p.id
+    and pa.post_id = OLD.post_id;
   END IF;
   return null;
 end $$;
@@ -91,11 +94,14 @@ begin
     where pa.post_id = NEW.post_id;
 
   ELSIF (TG_OP = 'DELETE') THEN
+    -- Join to post because that post may not exist anymore
     update post_aggregates pa
     set score = score - OLD.score,
     upvotes = case when OLD.score = 1 then upvotes - 1 else upvotes end,
     downvotes = case when OLD.score = -1 then downvotes - 1 else downvotes end
-    where pa.post_id = OLD.post_id;
+    from post p
+    where pa.post_id = p.id
+    and pa.post_id = OLD.post_id;
 
   END IF;
   return null;