cli icon indicating copy to clipboard operation
cli copied to clipboard

[Bug?] Views with non-nullable properties are nullable in generated types

Open xxRockOnxx opened this issue 3 years ago • 4 comments

Bug report

So as mentioned in the title: Views with non-nullable properties are nullable in generated types.

Describe the bug

Related StackOverflow question: https://stackoverflow.com/questions/17301323/why-are-my-views-columns-nullable

It seems like according to one of the answers:

The nullability tracking in PostgreSQL is not developed very much at all. In most places, it will default to claiming everything is potentially nullable, which is in many cases allowed by the relevant standards. This is the case here as well: Nullability is not tracked through views. I wouldn't rely on it for an application.

According to another one of the answers, you can also set the nullability manually.

The CLI seems to solely use Postgres as the source of truth.

That said, I'm not sure however if letting CLI check the base table would be an option.

It seems like either that or setting the attribute manually are the only options.

To Reproduce

  • Create a table
  • Create a view of that table
  • Generate type using supabase CLI

Expected behavior

Generated view properties are the same as the base table

System information

  • OS: Linux
  • Version of supabase-js: 2.0.4
  • Version of Node.js: 16.15.1

xxRockOnxx avatar Nov 02 '22 12:11 xxRockOnxx

Yeah, I think this is a limitation on the Postgres side as per the SO link. See also: https://github.com/supabase/cli/issues/385#issuecomment-1253370577

So the only option is probably setting the nullability manually. The typegen currently ignores that though - I'll keep this issue open until we fix that.

soedirgo avatar Nov 03 '22 03:11 soedirgo

This might help you @xxRockOnxx I wrote this helper utility type in typescript:

type NonNullableRow<Row, NullableKeys = never> = {
  [key in keyof Row]: key extends NullableKeys
    ? Row[key]
    : NonNullable<Row[key]>
}

It can be used like this:

type ProfileWithStats = NonNullableRow<
  Database['public']['Views']['profiles_with_stats']['Row'],
  'email_hash' | 'middle_name'
>

The second generic argument is optional, if you don't provide it, then all fields will be non nullable. But if some fields actually are nullable, then you can provide the list too.

enyo avatar Nov 08 '22 14:11 enyo

@soedirgo any info on when this might get added to the cli (it seems to be fixed already in postgres-meta?)

enyo avatar Dec 19 '22 08:12 enyo

This is still causing any and all view typings generated to be unusable

binury avatar Feb 14 '24 08:02 binury