supabase-community / postgrest-csharp

A C# Client library for Postgrest
https://supabase-community.github.io/postgrest-csharp/api/Postgrest.html
MIT License
114 stars 22 forks source link

About Insert Method #64

Closed Phantom-KNA closed 1 year ago

Phantom-KNA commented 1 year ago

Bug report

About Insert Method

Describe the bug

Get a Exception when at use.

Error

Unhandled exception. Postgrest.RequestException: null value in column "id" of relation "favorites" violates not-null constraint at Postgrest.Helpers.MakeRequest(ClientOptions clientOptions, HttpMethod method, String url, JsonSerializerSettings serializerSettings, Object data, Dictionary2 headers, CancellationToken cancellationToken) at Postgrest.Helpers.MakeRequest[T](ClientOptions clientOptions, HttpMethod method, String url, JsonSerializerSettings serializerSettings, Object data, Dictionary2 headers, Func`1 getHeaders, CancellationToken cancellationToken) at SupabaseExample.Program.Main(String[] args) in D:\Development\Example\Supabase SRC\supabase-csharp-0.6.2\Examples\SupabaseExample\Program.cs:line 39 at SupabaseExample.Program.

(String[] args)

System information

  • OS: [Windows]
  • Version of supabase-js: [e.g. 6.0.2]

Additional context

I try change the value, always is null.

acupofjose commented 1 year ago

@Phantom-KNA could you show the code for your model and the call you're attempting with postgrest?

acupofjose commented 1 year ago

@Phantom-KNA I would assume that it is an issue with your Model not being defined correctly.

Assuming your model is Favorites

[Table("favorites")]
public class Favorite: BaseModel
{
    [PrimaryKey("id")] // <-- Should be defined in Supabase Dashboard/Postgres to autogenerate this id
    public string Id { get; set; }

    [Column("reference_id")]
    public string ReferenceId { get; set; }

    [Column("is_favored")]
    public bool IsFavored { get; set; }
}

This will let you insert a model and ignore the Id field. If you're trying to generate the Id client side (not recommended) then change the model so that PrimaryKey("id", true)

Phantom-KNA commented 1 year ago

Thanks a lot, I'll try this. Thank you!!!!

Phantom-KNA commented 1 year ago

How did you assume that my model was a favorite??, nobody had that information??

acupofjose commented 1 year ago

In your error log: Unhandled exception. Postgrest.RequestException: null value in column "id" of relation "favorites" violates not-null constraint

Phantom-KNA commented 1 year ago

@Phantom-KNA I would assume that it is an issue with your Model not being defined correctly.

Assuming your model is Favorites

[Table("favorites")]
public class Favorite: BaseModel
{
    [PrimaryKey("id")] // <-- Should be defined in Supabase Dashboard/Postgres to autogenerate this id
    public string Id { get; set; }

    [Column("reference_id")]
    public string ReferenceId { get; set; }

    [Column("is_favored")]
    public bool IsFavored { get; set; }
}

This will let you insert a model and ignore the Id field. If you're trying to generate the Id client side (not recommended) then change the model so that PrimaryKey("id", true)

Why is it not recommended to generate this id, I mean, I am saving a string list and in the ID I am trying to save with the user ID that is obtained in this way: supabase.Auth.CurrentClient.Id

Phantom-KNA commented 1 year ago

@Phantom-KNA I would assume that it is an issue with your Model not being defined correctly.

Assuming your model is Favorites

[Table("favorites")]
public class Favorite: BaseModel
{
    [PrimaryKey("id")] // <-- Should be defined in Supabase Dashboard/Postgres to autogenerate this id
    public string Id { get; set; }

    [Column("reference_id")]
    public string ReferenceId { get; set; }

    [Column("is_favored")]
    public bool IsFavored { get; set; }
}

This will let you insert a model and ignore the Id field. If you're trying to generate the Id client side (not recommended) then change the model so that PrimaryKey("id", true)

Why is it not recommended to generate this id, I mean, I am saving a string list and in the ID I am trying to save with the user ID that is obtained in this way: supabase.Auth.CurrentClient.Id

This is my model:

Table("favorites")]
public class Favorite: BaseModel
{
    [PrimaryKey("id")]
    public string Id { get; set; }

    [Column("list_movie")]
    public List<string> ListMovie { get; set; }

    [Column("list_tv")]
    public List<string> { get; set; }
}

For string listings in the table I use jsonb, which should i use for id string or uuid?

acupofjose commented 1 year ago

Why is it not recommended to generate this id, I mean, I am saving a string list and in the ID I am trying to save with the user ID that is obtained in this way: supabase.Auth.CurrentClient.Id

Ah sorry, that's my preference, not a hard rule. I generally believe that the database should handle ID generation for new records, not clients. You are free to do as you will!

For string listings in the table I use jsonb, which should i use for id string or uuid?

In the c# model, you'll probably want to use string with that setup.

Phantom-KNA commented 1 year ago

Why is it not recommended to generate this id, I mean, I am saving a string list and in the ID I am trying to save with the user ID that is obtained in this way: supabase.Auth.CurrentClient.Id

Ah sorry, that's my preference, not a hard rule. I generally believe that the database should handle ID generation for new records, not clients. You are free to do as you will!

For string listings in the table I use jsonb, which should i use for id string or uuid?

In the c# model, you'll probably want to use string with that setup.

Thanks bro, use C# Guid as model type, thanks for the time!!

acupofjose commented 1 year ago

Happy to help!