Skip to content
Advertisement

Return result of CASE with Posgres SQL function

I am trying to write a simple SQL function that will be used as a computed field in Hasura.io platform.

CREATE OR REPLACE FUNCTION calling_mode(calling_row calling, hasura_session json)
RETURNS text AS $$
  SELECT CASE
    WHEN calling_row.seekerUserId = hasura_session ->> 'x-hasura-user-id' THEN 'seeker'
    WHEN calling_row.partnerUserId = hasura_session ->> 'x-hasura-user-id' THEN 'partner'
    ELSE 'none'
  END
$$ LANGUAGE sql STABLE;

This gives me an error missing FROM-clause entry for table "calling_row".

I tried looking through several docs, but it doesn’t make any sense to me.

Advertisement

Answer

Probably that causes upper case letters, so if your original column name is seekerUserId, then into your function postgres converts it as seekeruserid, and that column is not found.

Try column names into double quotes, like:

CREATE OR REPLACE FUNCTION calling_mode(calling_row calling, hasura_session json)
RETURNS text AS $$
  SELECT CASE
    WHEN calling_row."seekerUserId" = hasura_session ->> 'x-hasura-user-id' THEN 'seeker'
    WHEN calling_row."partnerUserId" = hasura_session ->> 'x-hasura-user-id' THEN 'partner'
    ELSE 'none'
  END
$$ LANGUAGE sql STABLE;
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement