Hi, sorry to pick up an old thread; I've just gotten around to implementing this and I can't seem to make it work: I get this error when I run it:
ERROR: current transaction is aborted, commands ignored until end of transaction block (SQLSTATE 25P02)
Here's my user_sync function:
$BODY$
DECLARE
BEGIN
/* Example implementation
In update event, check if a record for the current user ID already exists;
if not, create one - update otherwise.
In delete event, remove the user association but keep the user record.
*/
IF _event = 'UPDATED' THEN
IF (
SELECT id
FROM {ber_db}.[users]
WHERE user_id = _user.id
) IS NULL THEN
INSERT INTO {ber_db}.[users] (
firstname,
lastname,
email,
user_id
) VALUES (
_user.name_fore,
_user.name_sur,
_user.email,
_user.id
);
ELSE
UPDATE {ber_db}.[users]
SET
firstname = _user.name_fore,
lastname = _user.name_sur,
email = _user.email
WHERE user_id = _user.id;
END IF;
ELSE
UPDATE {ber_db}.[users]
SET user_id = NULL
WHERE user_id = _user.id;
END IF;
RETURN 0;
END;
$BODY$
My relation is called "users" and has the following attributes:
id, user_id(with unique index), firstname, lastname, email
all except ID are text. What am I doing wrong? Thanks in advance and merry Christmas!