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:
- Verified that the
filterlogic 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>';
Checked that the
user_idandScanTimeStampvalues match exactly between the code and database.Read the Supabase documentation on
update():- Used the
set()function to update theApiJsonOutputcolumn. - Ensured the
select()method is included to return the updated row.
- Used the
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:
- Why is the
updateoperation not updating the row and returning an empty result? - Is there anything wrong with how I am using
select()orfilter()in the query? - Are there any additional steps or configurations required to make the
updateoperation work?
Any help would be greatly appreciated!
Edit:
After posting the original question, I realized the following additional details:
Session Validation
The session is validated at the start of the operationAnother 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) } }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.