2 simular computed columns / fields for different roles behaving differently #4244
-
We have 2 computed columns. curl 'http://localhost/rest/products' \
--get \
--data-urlencode 'select=name:name,product_class,items!left(*),...product_types(currency,productTypeName:name)' \
--data-urlencode 'product_class=in.(electric,consumable)' \ {
"code": "PGRST200",
"details": "Searched for a foreign key relationship between 'products' and 'items' in the schema 'api', but no matches were found.",
"hint": "Perhaps you meant 'item_details_rowtype' instead of 'items'.",
"message": "Could not find a relationship between 'products' and 'items' in the schema cache"
} When I used The computed columns are defined as: CREATE OR REPLACE FUNCTION api.items(
api.products)
RETURNS SETOF api.item_rowtype
LANGUAGE sql
AS $BODY$
select i::api.item_rowtype
from trading.item_detail i
where i.product = $1.id
and (
(select request.is_admin()) OR
(request.has_required_scope(i.required_scope))
);
$BODY$;
ALTER FUNCTION api.items(api.products)
OWNER TO migrator; And CREATE OR REPLACE FUNCTION api.item_details(
api.products)
RETURNS SETOF api.item_details_rowtype
LANGUAGE sql
AS $BODY$
select i::api.item_details_rowtype
from trading.item_detail i
where i.product = $1.id
and (
(select request.is_admin()) OR
(request.has_required_scope(i.required_scope))
);
$BODY$;
ALTER FUNCTION api.item_details(api.products)
OWNER TO migrator; Whas complicates this a little I didn't realise the frontend team were using a library for some time that has made computed columns / fields as in the curl sample above. Though they have been using id:items->product,unitPrice:items:unit_price That does return the results. Though its my understanding that this My question is there a reason the Also please confirm if the We are using PostgREST v12.2.12 and Postgres v17.4 Edit: added question + PostgREST version. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Oh, this is interesting. We have two implementations for functions on table types: Computed fields and Computed relationships. According to your examples you want the latter so the correct syntax for that is: So how is the select relkind from pg_class where relname = 'item_rowtype'; If it's
Yes, this works when treating the function as a computed field (i.e.
It looks like it's working as an inner join 'cause the underlying query does this for the computed field in SELECT "api"."products".*,
"api"."products"."items"
FROM "api"."products"; Whereas, for a computed relationship, it does an explicit |
Beta Was this translation helpful? Give feedback.
Oh, this is interesting. We have two implementations for functions on table types: Computed fields and Computed relationships. According to your examples you want the latter so the correct syntax for that is:
/products?select=items(*)
, but this only works when theitems
function returns a table not a composite type for now ( see #3096).So how is the
api.item_rowtype
defined, is it aTABLE
or aTYPE
? To check this, execute:If it's
c
then it's a composite type, you must use a table to make this work. If it'sr
then it's a table and it should work, perhaps you need to reload the schema cache.