I am trying to write a PostgreSQL function that inserts data into a database and then gets some data and returns it. Here is the code:
CREATE OR REPLACE FUNCTION newTask(projectid api.objects.projectid%TYPE, predecessortaskid api.objects.predecessortaskid%TYPE, creatoruserid api.objects.creatoruserid%TYPE, title api.objects.title%TYPE, description api.objects.description%TYPE, deadline api.objects.deadline%TYPE, creationdate api.objects.creationdate%TYPE, issingletask api.tasks.issingletask%TYPE) RETURNS SETOF api.v_task AS $$ DECLARE v_objectid api.objects.objectid%TYPE; BEGIN INSERT INTO api.objects(objectid, projectid, predecessortaskid, creatoruserid, title, description, deadline, creationdate) VALUES (DEFAULT, projectid, predecessortaskid, creatoruserid, title, description, deadline, creationdate) RETURNING objectid INTO v_objectid; INSERT INTO api.tasks(objectid, issingletask) VALUES (v_objectid, issingletask); RETURN QUERY (SELECT * FROM api.v_task WHERE objectid = v_objectid); END; $$ LANGUAGE plpgsql;
objects and tasks are both tables, and v_task is a view that is a union of the two. The reason I am returning the data that I just inserted is because it has triggers on it.
So far so good. I use RETURNS SETOF api.v_task as my return type and RETURN QUERY (...) and therefore expect the result to look like SELECT from v_task (same columns with same data types). However, what really happens (exiting pgAdmin, same result from my node.js-application ):
SELECT newTask( CAST(NULL AS integer), CAST(NULL AS integer), 1, varchar 'a',varchar 'a', cast(NOW() as timestamp(0) without time zone), cast(NOW() as timestamp(0) without time zone), true); newtask api.v_task
Instead of several columns, the output is forced into one, separated by commas.
Since I already use a special record type, I cannot use the AS keyword to indicate the fields of my output.