Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

structure of query does not match function result type

After changing type (by casting) from bigint to text my PLPGSQL function stopped working. This is the error I am getting:

dev=> select * from app.get_companies(4,808739954140037) ;
NOTICE:  Data rows were NOT found (structure of query does not match function result type)
 company_id_str | company_name 
----------------+--------------
(0 rows)

dev=> 

And this is my function:

CREATE OR REPLACE FUNCTION app.get_companies(ident_id bigint,sess bigint)
RETURNS TABLE(company_id_str text,company_name text) as $$
DECLARE
    server_session bigint;
BEGIN
    select app.session.session from app.session where app.session.identity_id=ident_id and app.session.session=sess into server_session;
    IF FOUND
    THEN
        BEGIN
            RETURN QUERY SELECT quote_ident(app.company.company_id::text)::text as company_id_str,app.company.name as company_name FROM app.company,app.identcomp WHERE app.company.company_id=app.identcomp.company_id and app.identcomp.identity_id=ident_id;
        EXCEPTION
            WHEN OTHERS THEN
                RAISE NOTICE 'Data rows were NOT found (%)',SQLERRM;
                RETURN;

        END;
    ELSE
        RAISE NOTICE 'Session row was NOT found';
        RETURN;
    END IF;
END;
$$ LANGUAGE plpgsql;

Why is this error happening if I am applying the cast and I am defining the output as TABLE ?

The app.company table is defined as:

create table app.company (
    company_id          bigserial,
    date_inserted       timestamp,
    name                varchar(64)
);
like image 532
Nulik Avatar asked Mar 10 '23 09:03

Nulik


1 Answers

app.company.name is varchar(64) while company_name of returned table is text. Cast app.company.name to text.

If you do not catch an exception in the function body (what for?) you would get more verbose error message:

ERROR:  structure of query does not match function result type
DETAIL:  Returned type character varying(64) does not match expected type text in column 2.
like image 133
klin Avatar answered Mar 15 '23 06:03

klin