supabase / postgrest-js

Isomorphic JavaScript client for PostgREST.

Home Page:https://supabase.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Updating row returns error code PGRST116

vladgardus opened this issue · comments

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

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.

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

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

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