supabase-community / postgrest-csharp

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

Update method - no Route matched with those values #57

Closed Cizzl closed 1 year ago

Cizzl commented 1 year ago

Bug report

Describe the bug

I followed the instruction of the docs. When I try to use the Update method I get this error message:

{Postgrest.RequestException: no Route matched with those values
   at Postgrest.Helpers.MakeRequest(ClientOptions clientOptions, HttpMethod method, String url, JsonSerializerSettings serializerSettings, Object data, Dictionary`2 headers, CancellationToken cancellationToken)

I have no issue with inserting or selecting. Also turned of my policies to test it.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

ModeledResponse<Product> response = await Client.From<Product>().Get();
Product model = response.Models.FirstOrDefault();
model.Note = "Test";
ModeledResponse<Product> result = await model.Update<Product>();

Expected behavior

Value should have changed.

System information

acupofjose commented 1 year ago

Thanks for the bug report! Out of curiosity, does doing the following work?

ModeledResponse<Product> response = await Client.From<Product>().Get();
Product model = response.Models.FirstOrDefault();
model.Note = "Test";
var result = await Client.From<Product>().Update(model);
Cizzl commented 1 year ago

Thank you, it worked

agwanyaseen commented 1 year ago

I am also getting the same error when trying to run the project, with my supabase instance values,

var option = new ClientOptions();
option.Headers.Add("apikey", "apikey");

var client = new Postgrest.Client("https://<instanceName>.supabase.co", option);
// Get all Users
var users = await client.Table<User>().Get();

I even tried making api call, and got the same response image

acupofjose commented 1 year ago

Ah, if you're going to use just the plain postgrest-csharp client outside of supabase-csharp - you need to specify the REST endpoint see here for endpoints (the supabase-client handles all of these for you).

So you need the following:

var option = new ClientOptions();
option.Headers.Add("apikey", "apikey");

var client = new Postgrest.Client("https://<instanceName>.supabase.co/rest/v1", option);
// Get all Users
var users = await client.Table<User>().Get();
ConnorIllingworth commented 1 year ago

Hello, I was also get this error when I try and Update a row, but after doing what you suggested for Cizzl I get Status Code 200 OK, but nothing updates in Supabase

ModeledResponse<UserCategory> dbRows = await Authentication.Client.Postgrest
   .Table<UserCategory>()
   .Filter("id", Postgrest.Constants.Operator.Equals, model.Id.ToString())
   .Get();

var dbRow = dbRows.Models.First();
dbRow.Name = model.Name;
dbRow.IntervalString = model.IntervalString;
dbRow.Sound = model.Sound;

var result = await Authentication.Client.From<UserCategory>().Update(dbRow);
acupofjose commented 1 year ago

@ConnorIllingworth can you show me what your model looks like?

ConnorIllingworth commented 1 year ago

Yes, heres my model

    [Table("usercategories")]
    public class UserCategory : BaseModel
    {
        [PrimaryKey("id")]
        public Guid Id { get; set; }

        [Column("userid")]
        public Guid UserId { get; set; }

        [Column("created_at")]
        public DateTime CreatedAt { get; set; }

        [Column("sound")]
        public int Sound { get; set; }

        [Column("name")]
        public string Name { get; set; }

        [Column("interval")]
        public string IntervalString { get; set; }
    }
acupofjose commented 1 year ago

Try setting the following and inserting again please!

[PrimaryKey("id", false)]
 public Guid Id { get; set; }
acupofjose commented 1 year ago

Although, false will be the default for most use cases… so maybe it’s time to just set that as such in the PrimaryKey attribute.