postgrest-js icon indicating copy to clipboard operation
postgrest-js copied to clipboard

Type safety is not being adhered on insert queries

Open dvrfluxchat opened this issue 1 year ago • 4 comments

I have a insert query in my db which looks like this and I am using the typescript version of supabase-js.

await supaServiceClient
        .from("table_name")
        .insert({
          column1:value,
          column2:value,
          column3:value
        });

I changed the column "column1" to "newColumn1" but the above query did not throw any errors where there is no "column1". This lead to issues in production where this query stopped working as the insert was failing saying there is no column1 but the compiler did not throw any error. Is this the expected behaviour? If so how do i ensure to get compile time warnings for such scenarios.

dvrfluxchat avatar Nov 28 '23 02:11 dvrfluxchat

I have the same issue. Did you find out @dvrfluxchat ?

tecoad avatar May 04 '24 08:05 tecoad

You will need call .select(...) after .insert(...), to specify the field you wish to be included to the data. With your exmaple if you wish to retrieve the values of all columns of the row created, use following code:

const {data, error} = await supaServiceClient
        .from("table_name")
        .insert({
          column1:value,
          column2:value,
          column3:value
        }).select("*");

You will then be able to access value of a column, let's say for exmaple column1

data?.[0].column1

weilantian avatar Jun 21 '24 14:06 weilantian

I am experiencing the same issue. There is no type safety on insert for values that are not part of your table schema.

E.g

const {data, error} = await supaServiceClient
        .from("table_name")
        .insert({
          column1:value, // this exists as a column in table_name
          column2:value, // this exists too
          column3:value // this does not
        }).select("*");

I think It should throw a type error letting you know that there is no column3 column in your table.

FilipPano avatar Aug 10 '24 19:08 FilipPano