Skip to content

Instantly share code, notes, and snippets.

@libovness
Created April 11, 2021 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save libovness/cf006846fda90bb160be427f2fdd06ae to your computer and use it in GitHub Desktop.
Save libovness/cf006846fda90bb160be427f2fdd06ae to your computer and use it in GitHub Desktop.
// Deletes the image object in storage when the corresponding record is deleted
CREATE OR REPLACE FUNCTION delete_corresponding_image() RETURNS trigger AS
$$BEGIN
DELETE
FROM storage.objects o
LEFT JOIN public.images ON i.id = o.image_id
RETURN NULL;
END;$$ LANGUAGE plpgsql;
CREATE TRIGGER delete_image_object_when_corresponding_record_is_deleted
BEFORE DELETE ON public.images FOR EACH ROW
EXECUTE PROCEDURE delete_corresponding_image();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment