supabase / supabase-js

An isomorphic Javascript client for Supabase. Query your Supabase database, subscribe to realtime events, upload and download files, browse typescript examples, invoke postgres functions via rpc, invoke supabase edge functions, query pgvector.
https://supabase.com
MIT License
2.86k stars 220 forks source link

Updates are not working #26

Closed zlwaterfield closed 3 years ago

zlwaterfield commented 3 years ago

Bug report

Describe the bug

I am running an update on a single row in the table wedding and it is 404ing. I've confirmed I am using the correct id and table. It seems the request being made is not correct.

Here is the code:

  async update(weddingId, wedding) {
    // weddingId = c81c8ab3-9fc5-41d6-9a68-843109ba95b0
    // wedding = {slug: "yes-"}
    return await SupabaseService
      .from<Wedding>('wedding')
      .update(wedding)
      .eq('id', weddingId);
  }

The request url is:

https://wqzrlmhkwwtfqmbwirha.supabase.co/rest/v1/wedding?slug=eq.y&id=neq.c81c8ab3-9fc5-41d6-9a68-843109ba95b0&slug=eq.ye&id=neq.c81c8ab3-9fc5-41d6-9a68-843109ba95b0&slug=eq.yes&id=neq.c81c8ab3-9fc5-41d6-9a68-843109ba95b0&slug=eq.yes-&id=neq.c81c8ab3-9fc5-41d6-9a68-843109ba95b0&id=eq.c81c8ab3-9fc5-41d6-9a68-843109ba95b0

If you break that down:

> queryString.parse('?slug=eq.y&id=neq.c81c8ab3-9fc5-41d6-9a68-843109ba95b0&slug=eq.ye&id=neq.c81c8ab3-9fc5-41d6-9a68-843109ba95b0&slug=eq.yes&id=neq.c81c8ab3-9fc5-41d6-9a68-843109ba95b0&slug=eq.yes-&id=neq.c81c8ab3-9fc5-41d6-9a68-843109ba95b0&id=eq.c81c8ab3-9fc5-41d6-9a68-843109ba95b0')
[Object: null prototype] {
  '?slug': 'eq.y',
  id: [
    'neq.c81c8ab3-9fc5-41d6-9a68-843109ba95b0',
    'neq.c81c8ab3-9fc5-41d6-9a68-843109ba95b0',
    'neq.c81c8ab3-9fc5-41d6-9a68-843109ba95b0',
    'neq.c81c8ab3-9fc5-41d6-9a68-843109ba95b0',
    'eq.c81c8ab3-9fc5-41d6-9a68-843109ba95b0'
  ],
  slug: [ 'eq.ye', 'eq.yes', 'eq.yes-' ]
}

Why are both neq and eq being applied and why is neq there 4 times? Also, why is slug there? The slug was not included in the filter, just in the body.

Screenshots

Screen Shot 2020-08-31 at 10 54 35 AM

System information

zlwaterfield commented 3 years ago

I will close this for now, I haven’t experienced this since I opened it and I am not sure how it happened. If I see it again I will create a repo to replicate it.

kiwicopple commented 3 years ago

Here were the ideas discussed on Slack, in case it gets reopened:

surjithctly commented 2 years ago

What's the possible fix for this? I'm getting same 404 error on a Next.js app. I tried after yarn build but same.

KevTale commented 2 years ago

I'm having the same behaviour with update method and I'm also using Next.js.

    let { data: profiles, error } = await supabase
        .from("profiles")
        .select("*")
        .eq("id", "f4a2d4dd-4aec-4af6-8ba9-9556be7349c3");

works fine.

But this does not (404 error):

     let { data: profiles, error } = await supabase
        .from("profiles")
        .update({ username })
        .eq("id", "f4a2d4dd-4aec-4af6-8ba9-9556be7349c3");

Having a 404 error. The endpoint is https://url.supabase.co/rest/v1/profiles?id=eq.f4a2d4dd-4aec-4af6-8ba9-9556be7349c3

KushibikiMashu commented 2 years ago

Same here. I'm using "@supabase/supabase-js": "^1.24.0" with Next.js.

This works.

const { data, error } = await supabase
  .from('post_likes')
  .select()
  .match({ post_id: postId, account_id: accountId })
// console.log(data)
[
    {
        "postId": 1,
        "accountId": 1,
        "read": false
    }
]

But this doesn't work.

const { data, error } = await supabase
  .from('post_likes')
  .update({
    read: true,
  })
  .match({ post_id: postId, account_id: accountId })
// console.log(data, error)
null []

The response status of PUT /rest/v1/post_likes?post_id=eq.1&account_id=eq.1 is 404.

GET and DELETE methods are ok.

KushibikiMashu commented 2 years ago

I solved this problem by myself. There was something wrong with the update policy.

create policy "Users can update their own like."
  on comment_likes for update with check ( auth.role() = 'authenticated' );

I changed with check to using.

create policy "Users can update their own like."
  on comment_likes for update using ( auth.role() = 'authenticated' );

Then, update from JS works.

AgarwalPragy commented 2 years ago

For anyone facing similar issues with other update queries, please note that

https://www.postgresql.org/docs/current/sql-createpolicy.html

EskelCz commented 1 year ago

@AgarwalPragy You mean INSERT or UPSERT, right? I thought that UPDATE cannot add a new row.

AgarwalPragy commented 1 year ago

@AgarwalPragy You mean INSERT or UPSERT, right? I thought that UPDATE cannot add a new row.

That is a little confusing actually. I just quoted the line direct from the postgres docs: https://www.postgresql.org/docs/current/sql-createpolicy.html

It explicitly says

... while new rows that would be created via INSERT or UPDATE are checked against the expression specified in WITH CHECK ...

Probably means "rows that are created as a side effect of the update operation" - in case of triggers for example.