SupabaseS
Supabase2y ago
Kaue

How to return table row on RPC function?

Hey all, I'm writing an RPC function that does a bunch of changes in sequence and I want to return a created row from the RPC but I don't know how.

Here's my current implementation. As a result of the RPC function I'd like to return the row created by INSERT INTO properties (name, description, organization, created_at):

create or replace function create_property (
  property_name text,
  property_description text,
  property_organization bigint,
  address_location geography,
  address_city text,
  address_country text,
  address_district text,
  address_formatted text,
  address_housenumber text,
  address_lat numeric,
  address_lon numeric,
  address_organization bigint,
  address_postcode text,
  address_state text,
  address_street text,
  address_suburb text
) returns bigint as $BODY$
DECLARE
    property_id bigint;
    address_id bigint;
BEGIN
    INSERT INTO properties (name, description, organization, created_at)
    VALUES (property_name, property_description, property_organization, now())
    RETURNING id INTO property_id;
    INSERT INTO property_addresses (location, property, city, country, district, formatted, housenumber, lat, lon, organization, postcode, state, street, suburb)
    VALUES (address_location, property_id, address_city, address_country, address_district, address_formatted, address_housenumber, address_lat, address_lon, address_organization, address_postcode, address_state, address_street, address_suburb)
    RETURNING id INTO address_id;
    RETURN property_id;
END;
$BODY$ language plpgsql;
Was this page helpful?