0

I am trying to update a row in my Supabase database using Kotlin. However, the update operation always returns an empty list, and the update does not reflect in the database. I have verified that the SQL query with the same filter works perfectly in the Supabase query editor.

Here's the relevant code snippet:

val apijson = supabase.from("Table_User_Scans").update({
    set("ApiJsonOutput", jsonString)
}) {
    select()
    filter {
        and {
            eq("user_id", userId)
            eq("ScanTimeStamp", LatestScanDate)
        }
    }
}.decodeSingle<UserScans>()

UserScans Data Class:

package com.indiedev91.looksmaxeai_bebetterversion.Utils

import kotlinx.serialization.Serializable

@Serializable
data class UserScans( 
    val user_id: String,
    val UserName: String,
    val ScanTimeStamp: String, 
    val FrontFaceImageUrl: String,
    val SideFaceImageUrl: String,
    val ApiJsonOutput: String? 
)

Database Schema:

Name Description Data Type Format
user_id No description uuid uuid
UserName No description character varying varchar
ScanTimeStamp No description timestamp without time zone timestamp
FrontFaceImageUrl No description character varying varchar
SideFaceImageUrl No description character varying varchar
ApiJsonOutput No description character varying varchar

What I Have Tried:

  1. Verified that the filter logic is correct. The SQL query below works perfectly when executed in the Supabase editor:
UPDATE "Table_User_Scans"
SET "ApiJsonOutput" = 'helloworld'
WHERE "ScanTimeStamp" = '2025-01-16 21:19:40' 
  AND "user_id" = '<UserID>';
  1. Checked that the user_id and ScanTimeStamp values match exactly between the code and database.

  2. Read the Supabase documentation on update():

    • Used the set() function to update the ApiJsonOutput column.
    • Ensured the select() method is included to return the updated row.

Observations:

  • The query always returns an empty list.
  • The update operation does not modify the row in the database.

Relevant Documentation:

According to the Supabase Kotlin SDK documentation, I am following the correct procedure for using update() with filter() and select().

My Questions:

  1. Why is the update operation not updating the row and returning an empty result?
  2. Is there anything wrong with how I am using select() or filter() in the query?
  3. Are there any additional steps or configurations required to make the update operation work?

Any help would be greatly appreciated!

Edit:

After posting the original question, I realized the following additional details:

  1. Session Validation
    The session is validated at the start of the operation

  2. Another Update Operation Working Fine
    An update operation on another table (Table_User_Data) with a similar approach works perfectly, as shown below:

    supabase.from("Table_User_Data").update({
        set("LatestScanDate", LatestScanDate)
    }) {
        filter { eq("user_id", userId) }
    }
    
  3. Complete Code Context
    The issue occurs within the following complete code:

    class SupabaseDatabaseUpdater {
        private val supabase = SupabaseClientProvider.client
    
        fun updateTableWithJson(userId: String, jsonData: JSONObject) {
            try {
                GlobalScope.launch {
                    updateTableWithJson_Suspended(userId, jsonData)
                }
            } catch (e: Exception) {
                println("Error: ${e.message}")
            }
        }
    
        suspend fun updateTableWithJson_Suspended(userId: String, jsonData: JSONObject) {
            try {
                val session = supabase.auth.currentSessionOrNull()
                if (session != null) {
                    val latestScanData = withContext(Dispatchers.IO) {
                        supabase.from("Table_User_Scans").select {
                            filter { eq("user_id", userId) }
                            order(column = "ScanTimeStamp", order = Order.DESCENDING)
                            limit(count = 1)
                        }.decodeSingle<UserScans>()
                    }
    
                    val LatestScanDate = latestScanData.ScanTimeStamp
    
                    // This update works perfectly
                    supabase.from("Table_User_Data").update({
                        set("LatestScanDate", LatestScanDate)
                    }) {
                        filter { eq("user_id", userId) }
                    }
    
                    // The issue occurs here: update on "Table_User_Scans"
                    val jsonString = jsonData.toString()
                    val apijson = supabase.from("Table_User_Scans").update({
                        set("ApiJsonOutput", jsonString)
                    }) {
                        select()
                        filter {
                            and {
                                eq("user_id", userId)
                                eq("ScanTimeStamp", LatestScanDate.replace(" ", "T"))
                            }
                        }
                    }.decodeSingle<UserScans>()
    
                    if (apijson != null) {
                        Log.d("ApiResponeUpdate", "ApiJson is updated on supabase")
                    }
                } else {
                    println("User is not authenticated")
                }
            } catch (e: Exception) {
                println("Error: ${e.message}")
            }
        }
    }
    

Despite the similarities between the two update operations, only the first one (Table_User_Data) works, while the second one (Table_User_Scans) fails to update and always returns an empty list.

This additional context might provide more clarity on the issue.

1
  • Okay guys , probably a dumb way to learn , to first create the rls policies on the tables then start working Commented Jan 19 at 12:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.