postgresqlschemarpcsupabase

PGRST202 Error when calling RPC with passed in parameter


I'm getting a 400 Bad request error when calling an RPC that has a string passed to it. The aim of the function is to return the row(s) where the searchquery matches the suburb_name in Supabase.

ERROR:

{"code":"PGRST202","details":"Searched for the function public.get_suburb_data without parameters, but no matches were found in the schema cache.","hint":null,"message":"Could not find the function public.get_suburb_data without parameters in the schema cache"}

page.tsx:

`"use client";
import { GetSummarySuburbData } from "@/app/database.types";
import { supaClient } from "@/app/supa-client";
import { useEffect, useState } from "react";

// Get Suburb Name from URL
function getSuburbNameFromURL() {
    const url = new URL(window.location.href);
    const pathname = url.pathname;
    const stringInURL = pathname.replace("/suburb/", "");
    const suburbInURL = stringInURL.replace(/&/g, " ");
    return suburbInURL;
}

export default function GetSummaryData() {
    const [suburbName, setSuburbName] = useState("");
    const [summaryData, setSummaryData] = useState<GetSummarySuburbData[]>([]);

    useEffect(() => {
        const searchQuery = getSuburbNameFromURL();
        console.log(searchQuery);
        setSuburbName(String(searchQuery));
    }, []);

    useEffect(() => {
        if (suburbName) {
            supaClient.rpc("get_suburb_data", { searchquery: suburbName }).then(({ data }) => {
                setSummaryData(data as GetSummarySuburbData[]);
            });
        }
    }, [suburbName]);

    return (
        <>
            Test
            <div>
                {summaryData?.map((data) => (
                    <p key={data.id}>{data.suburb_name}</p>
                ))}
            </div>
        </>
    );
}`

PostgreSQL:

-- Return row where suburb_name matches searchQuery
CREATE FUNCTION get_suburb_data("searchquery" text)
RETURNS table (
id uuid,
suburb_name text,
state_name text,
post_code numeric,
people INT,
male REAL,
female REAL,
median_age INT,
families INT,
average_number_of_children_per_family text,
for_families_with_children REAL,
for_all_households REAL,
all_private_dwellings INT,
average_number_of_people_per_household REAL,
median_weekly_household_income INT,
median_monthly_mortgage_repayments INT,
median_weekly_rent_b INT,
average_number_of_motor_vehicles_per_dwelling REAL
)
LANGUAGE plpgsql
AS $$
begin
return query
select id, suburb_name, state_name, post_code, people, male, female, median_age, families,
average_number_of_children_per_family, for_families_with_children, for_all_households,
all_private_dwellings, average_number_of_people_per_household, median_weekly_household_income,
median_monthly_mortgage_repayments, median_weekly_rent_b, average_number_of_motor_vehicles_per_dwelling
from summary_data
WHERE suburb_name ILIKE '%' || searchquery || '%'
AND summary_data.path \~ "root";
end;$$;

Solution

  • If you're using Supabase, the schema cache in Postgres is automatically updated. If you'd like to do it manually just to be sure you can run

    NOTIFY pgrst, 'reload schema'
    

    in SQL to make PostgREST reload it.

    For your code sample

    supaClient.rpc("get_suburb_data", { searchquery: suburbName })
    

    It looks like you're only passing a suburbName, which is not compatible with the SQL function that takes 18 arguments. You'll either need to pass values for all arguments or update the function to provide a default value (the default can be null).