8000 Make function return types nullable by TomasHubelbauer · Pull Request #480 · supabase/postgres-meta · GitHub
[go: up one dir, main page]

Skip to content

Make function 8000 return types nullable #480

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

Merged
merged 1 commit into from
Jan 28, 2023
Merged

Make function return types nullable #480

merged 1 commit into from
Jan 28, 2023

Conversation

TomasHubelbauer
Copy link
Contributor

What kind of change does this PR introduce?

Makes all table function column return types nullable.

What is the current behavior?

The verbatim function types are used as column types despite the functions potentially returning null.

What is the new behavior?

All return types are treated as nullable.

Additional context

@psteinroe contributed a change which adds schema functions which take only the table name as columns on the tables when generating TypeScript types.

This commit extends that to treat function return types as nullable.

The function used in oo-init.sql could be changed like so:

create function public.blurb(public.todos) returns text as
$$
select null;
$$ language sql stable;

It would still compile and be a valid function despite the unchanged return type, which means any function must be treated as potentially returning null in the types.

I have also updated instructions for building and testing the TypeScript type generation.

Here's how I tested this:

  • Run npm install and npm run dev to spin up the Docker container and the code watcher
  • Run npm run gen:types:typescript to hit the endpoint that generates the TypeScript types
  • Make the change in src/server/templates/typescript.ts and re-run the above
  • Check the output of the above command to see what the new generated response looks like

@TomasHubelbauer TomasHubelbauer requested a review from a team as a code owner January 20, 2023 20:30
@sweatybridge
Copy link
Contributor

It would still compile and be a valid function despite the unchanged return type, which means any function must be treated as potentially returning null in the types.

I wonder if there's a way to force postgres to throw an error when such functions are defined?

If a function returns a column that is marked as not nullable, it's a bit confusing to have a nullable function return type.

@psteinroe contributed a change which adds schema functions which take only the table name as columns on the tables when generating TypeScript types.

This commit extends that to treat function return types as nullable.

The function used in `oo-init.sql` could be changed like so:

```sql
create function public.blurb(public.todos) returns text as
$$
select null;
$$ language sql stable;
```

It would still compile and be a valid function despite the unchanged return type, which means any function must be treated as potentially returning `null` in the types.

I have also updated instructions for building and testing the TypeScript type generation.
@soedirgo
Copy link
Member

Thanks for the fix!

If a function returns a column that is marked as not nullable

It's not really a column - it's a regular function that takes a table's row type as the argument, so the nullability information isn't stored anywhere. We only treat them as columns because PostgREST exposes them as if they were (generated) columns.

There's also the actual generated columns - that should be preferred since that fulfills the same purpose and AFAIK allows you to set nullability.

@soedirgo soedirgo merged commit 95b449a into supabase:master Jan 28, 2023
@TomasHubelbauer TomasHubelbauer deleted the make-fn-return-types-nullable branch January 28, 2023 12:27
@github-actions
Copy link

🎉 This PR is included in version 0.60.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

avallete pushed a commit that referenced this pull request May 13, 2025
psteinroe contributed a change which adds schema functions which take only the table name as columns on the tables when generating TypeScript types.

This commit extends that to treat function return types as nullable.

The function used in `oo-init.sql` could be changed like so:

```sql
create function public.blurb(public.todos) returns text as
$$
select null;
$$ language sql stable;
```

It would still compile and be a valid function despite the unchanged return type, which means any function must be treated as potentially returning `null` in the types.

I have also updated instructions for building and testing the TypeScript type generation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0