0

I am trying to check if a userhandle already exists in the database as part of registration process of my users (the userhandle has to be unique). I am using Hasura and trying to write a custom SQL function that should return a boolean (true or false if the userhandle already exists).

I am getting errors at "EXISTS" here, can I somehow use EXISTS to return a boolean if the userhandle exists in the table or not? In Hasura I have to return a BASE type, which means Boolean should be OK (I think). Would greatly appreciate any help!

CREATE FUNCTION userhandle_exist(users_row users, search text)
RETURNS BOOLEAN AS $$
  EXISTS(SELECT * FROM users WHERE users.userhandle = search)
$$ LANGUAGE sql STABLE;
Svarto
  • 603
  • 2
  • 8
  • 20

1 Answers1

0

you didn't select the result of the exists operator. So, it should be like:

CREATE FUNCTION userhandle_exist(users_row users, search text)
RETURNS BOOLEAN AS $$
SELECT EXISTS(SELECT * FROM users WHERE users.userhandle = search)
LANGUAGE sql STABLE;

you can then access the function through the sql SELECT.