Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates are not working #26

Closed
zlwaterfield opened this issue Aug 31, 2020 · 10 comments
Closed

Updates are not working #26

zlwaterfield opened this issue Aug 31, 2020 · 10 comments
Labels
bug Something isn't working

Comments

@zlwaterfield
Copy link
Contributor

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

  • Version of supabase-js: v0.36.4
  • Version of Node.js: v12.12.0
@zlwaterfield zlwaterfield added the bug Something isn't working label Aug 31, 2020
@zlwaterfield
Copy link
Contributor Author

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
Copy link
Member

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

  • it could be caused by the way the library / code is set up - SupabaseService indicates that you’re somehow adding filters to the query from some other area of your app before executing the call with select() / update() etc.
  • could be caused by an auto-reloading dev environment and that is somehow caching the filters each reload

@surjithctly
Copy link

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
Copy link

KevTale commented Oct 11, 2021

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
Copy link

KushibikiMashu commented Oct 24, 2021

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
Copy link

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
Copy link

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

  • Existing table rows are checked against the expression specified in USING
  • New rows that would be created via INSERT or UPDATE are checked against the expression specified in WITH CHECK

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

@EskelCz
Copy link

EskelCz commented Jul 30, 2022

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

@AgarwalPragy
Copy link

AgarwalPragy commented Jul 30, 2022

@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.

@ukpagrace
Copy link

ukpagrace commented Aug 26, 2024

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.

Thanks, changed mine from this
create policy "Allow update access" on public."QnA" for update with check ( true );
to this
create policy "Allow update access" on public."QnA" for update using ( true );

and it worked, what's the difference?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

8 participants