supabase / postgrest-js

Isomorphic JavaScript client for PostgREST.
https://supabase.com
MIT License
965 stars 129 forks source link

Updating row returns error code PGRST116 #431

Open vladgardus opened 1 year ago

vladgardus commented 1 year ago

Bug report

Describe the bug

I encountered this issue only when the edge function I'm using is deployed. I've written some code where I insert a record and a few lines of code after I update the same record. It happens sometimes that updates return this error:

{
  code: "PGRST116",
  details: "Results contain 0 rows, application/vnd.pgrst.object+json requires 1 row",
  hint: null,
  message: "JSON object requested, multiple (or no) rows returned"
}

This is in mainly the code compiled from multiple files:

import { Database } from '../../database.types.ts';
import { supabaseConfig } from './config.ts';
import { createClient } from '@supabase/supabase-js';
const supabaseClient = createClient<Database>(supabaseConfig.SB_URL, supabaseConfig.SB_KEY);
const chaptersDB = supabaseClient.from('chapter');
const { data: chapter, error } = await chaptersDB
  .insert({
    --columns here--
  })
  .select()
  .single();
const { id } = chapter;
const { error } = await chaptersDB.update({ --columns here-- }).eq('id', id);
const { error } = await chaptersDB.update({ --other columns here-- }).eq('id', id);

What am I doing wrong?

It feels like those queries run in different transactions and the first one doesn't end after the insert.

Do I need to somehow destroy the supabase client after each edge function execution? This does not happen right after deploying the function, but after a couple of runs.

bitlab-code commented 9 months ago

same error here, with update. posteresti-js v1.7.2

bitlab-code commented 9 months ago

After investigating we notice that the problem was a computed field (in the select) that returns null.

Rewrite .select('*, computed_field') into .select('*, computed_field(*)') solve the issue for us.

@vladgardus check if this is your case

winoooops commented 7 months ago

FYI, Same here with udpate, I fixed it by updating the policies.