I have the following trigger which is supposed to assign number to a post every time a new record is inserted into the posts table. It finds the max number of posts scoped to the project_id of the new record.
CREATE OR REPLACE FUNCTION assign_number()
RETURNS trigger AS
$BODY$
BEGIN
SELECT coalesce(MAX(number), 0) max_number FROM posts WHERE project_id = NEW.project_id;
NEW.number := max_number + 1;
RETURN NEW;
END;
$BODY$ LANGUAGE plpgsql;
CREATE TRIGGER post_created
BEFORE INSERT ON posts
FOR EACH ROW
EXECUTE PROCEDURE assign_number();
Unfortunately, I get the syntax error query has no destination for result data when trying to do an INSERT. I've tried everything I can think of and looked at every related question on StackOverflow, but still do not seem to be able to figure this out.
Any ideas on what's wrong here? Anything obvious that I'm missing?
INTO?