Created
August 10, 2023 21:34
-
-
Save kaushik94/5257f0ff5520598e6327d148056ff3bb to your computer and use it in GitHub Desktop.
SQL Trigger to pre-insert user_id into message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE FUNCTION insert_user_id() | |
RETURNS trigger AS $BODY$ | |
BEGIN | |
SELECT id INTO NEW.to_id FROM users WHERE email = NEW.to_email; | |
RETURN NEW; | |
END; | |
$BODY$ LANGUAGE plpgsql; | |
CREATE TRIGGER insert_article BEFORE INSERT OR UPDATE ON messages FOR EACH ROW EXECUTE PROCEDURE insert_user_id(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment