-
-
Notifications
You must be signed in to change notification settings - Fork 144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
result types and computed field #559
Comments
Just tried out with both a computed field and a generated column defined like so: CREATE TYPE public.user_status AS ENUM ('ONLINE', 'OFFLINE');
CREATE TABLE public.users (
username text primary key,
data jsonb DEFAULT null,
age_range int4range DEFAULT null,
status user_status DEFAULT 'ONLINE'::public.user_status,
catchphrase tsvector DEFAULT null,
-- Simple immutable expression using just the username
generated_col text GENERATED ALWAYS AS (username || '_generated') STORED
);
ALTER TABLE public.users REPLICA IDENTITY FULL; -- Send "previous data" to supabase
COMMENT ON COLUMN public.users.data IS 'For unstructured data and prototyping.';
-- Create a computed field
CREATE FUNCTION username_status(public.users)
RETURNS text as $$
SELECT $1.username || ' ' || $1.status::text;
$$ LANGUAGE SQL; The resulting "Row" type after types gens seems correct: ...
users: {
Row: {
age_range: unknown | null
catchphrase: unknown | null
data: Json | null
generated_col: string | null
status: Database["public"]["Enums"]["user_status"] | null
username: string
username_status: string | null
}
... @whollacsek Did you regenerated the types recently ? This should already work with the current query parser. |
This is the generated type I got: export type Database = {
event_ticketing: {
Functions: {
sold_quantity: {
Args: {
inventory_item: unknown
}
Returns: number
}
... I think the current parser or type generator doesn't understand schemas other than public as you see the |
PostgREST can indeed cause issues with cross-schema relationships. Additionally, computed fields need to be declared in the exposed schema to be used as expected:
It could also be related to how the function is defined: supabase/postgres-meta#775. A potential workaround would be to manually override the types and declare the column in the appropriate "Row", as shown in the second example here: https://supabase.com/docs/guides/api/rest/generating-types#helper-types-for-tables-and-joins Anyway if introspection gets confused, the parser will inevitably misinterpret it. The discussion belongs to |
I see thanks for explaining, in my case the function and the table are both in the same schema and this schema is exposed. For now I'll implement the workaround you linked, hopefully this could be solved in the future. |
Indeed, for what I understood your function arguments might come from another schema is that it ? What could be really helpful is if you could provide a minimal SQL example of how we can reproduce that with a simple Something like: CREATE TABLE event_ticketing ( ...<minimal set of columns>... )
CREATE FUNCTION sold_quantity(...) With the exact function definition that could just return a simple dummy value. The actual content of the function shouldn't really impact the introspection. But I'm thinking maybe something special about the prototype definition ? If we can get the full pipeline from SQL to the failing types, we should be able to reproduce and debug this much better. |
Closing seems like duplicate of #474 |
Originally posted by @whollacsek in #558 (comment)
The text was updated successfully, but these errors were encountered: