Code returns an error because supabase-kt adds an underscore between my column string in V1.1.0 #222
-
Hi! I have a problem with the current version of supabase-kt, as I just updated earlier today. Weirdly, when adding a new row of data to the database there seems to be no problem, however when updating an existing row of data there pops up an error immediately, saying I was using the wrong column name (I didn't) then suggested to use the right column name (which I am using). Here's the code: package com.unovil.tardyscanner
import android.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.core.view.isVisible
import com.unovil.tardyscanner.addrecord.TABLE_NAME
import com.unovil.tardyscanner.databinding.ActivitySuccessfulBinding
import io.github.jan.supabase.SupabaseClient
import io.github.jan.supabase.createSupabaseClient
import io.github.jan.supabase.exceptions.HttpRequestException
import io.github.jan.supabase.postgrest.Postgrest
import io.github.jan.supabase.postgrest.postgrest
import io.github.jan.supabase.postgrest.query.Returning
import io.ktor.client.plugins.*
import kotlinx.coroutines.*
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlinx.serialization.Contextual
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.*
import java.text.SimpleDateFormat
import java.util.*
class SuccessfulActivity : ComponentActivity(), View.OnClickListener {
companion object {
const val tardyListID = "tardy_datetimes"
}
// initializes supabase connection
private lateinit var client: SupabaseClient
// format of Students table
@Serializable
data class Tardy(
@SerialName("lrn_id") val lrnId: String,
val name: String,
val section: String,
@SerialName(tardyListID) @Contextual val tardyDateTimes: List<Instant>
)
private lateinit var binding: ActivitySuccessfulBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySuccessfulBinding.inflate(layoutInflater)
setContentView(binding.root)
client = createSupabaseClient(
supabaseUrl = intent.getStringExtra("SUPABASE_URL") ?: "",
supabaseKey = intent.getStringExtra("SUPABASE_KEY") ?: ""
) {
install(Postgrest)
}
// ...some unimportant stuff omitted related to setting text on screen, assume all variables have been declared
// continually tries to update or insert to supabase
// if job is completed, enable button behavior
// if caught in a request timeout, repeat
var isSuccessful: Boolean
while (true) {
val jobGet = CoroutineScope(Dispatchers.IO).launch { updateDatabase(timeInstantKotlin) }
runBlocking {
jobGet.join()
if (jobGet.isCompleted) {
binding.confirmButton.isVisible = true
binding.confirmButton.isEnabled = true
isSuccessful = true
} else isSuccessful = false
}
if (isSuccessful) { break }
else {
runOnUiThread {
Toast.makeText(
this,
"Something went wrong! Trying again...",
Toast.LENGTH_SHORT).show()
}
continue
}
}
}
private suspend fun updateDatabase(timeInstant: Instant) {
return withContext(Dispatchers.IO) {
while (true) {
try {
// fetches data
val selectResult = client.postgrest[TABLE_NAME]
.select {
Tardy::lrnId eq intent.getStringExtra("lrn")
}
// checks if record number is not 1.
// if true (it's 2), insert new record.
// if false (it's not 2), update current record.
if (selectResult.body!!.toString().length == 2) { // no record yet
val insertResult = client.postgrest[TABLE_NAME]
.insert(
Tardy(
intent.getStringExtra("lrn") ?: "default",
intent.getStringExtra("name") ?: "default",
intent.getStringExtra("section") ?: "default",
listOf(timeInstant)
)
)
Log.i("SuccessfulActivity", "Insert headers: ${insertResult.headers}")
Log.i("SuccessfulActivity", "Insert body: ${insertResult.body}")
} else {
val tardyList =
selectResult.body!!.jsonArray[0].jsonObject[tardyListID]?.let {
Json.decodeFromJsonElement<List<Instant>>(it)
}
val updateResult = client.postgrest[TABLE_NAME]
.update(
returning = Returning.REPRESENTATION,
update = {
Tardy::tardyDateTimes setTo tardyList?.plusElement(timeInstant)
}
) {
Tardy::lrnId eq intent.getStringExtra("lrn")
}
Log.i("SuccessfulActivity", "Update headers: ${updateResult.headers}")
Log.i("SuccessfulActivity", "Update body: ${updateResult.body}")
}
break
} catch (hrtex: HttpRequestTimeoutException) {
runOnUiThread {
Toast.makeText(
this@SuccessfulActivity,
"Request timeout! Retrying...",
Toast.LENGTH_SHORT
).show()
}
} catch (hrex: HttpRequestException) {
val builder = AlertDialog.Builder(this@SuccessfulActivity)
runOnUiThread {
builder
.setTitle("Internet problem")
.setMessage("You are not connected to the Internet. Relaunch the app with Internet.")
.setPositiveButton("OK") { _, _ ->
val intent = Intent(this@SuccessfulActivity, MainActivity::class.java)
this@SuccessfulActivity.startActivity(intent)
finishAffinity()
}
.show()
}
break
}
}
}
}
} The code throws this error but only when I'm updating an existing record:
As you can see, the error says I put "tardy_date_times" when I put "tardy_datetimes" (the actual, correct one) in the code. Any help here? Thanks 😢 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Can you change this property when installing Postgrest: install(Postgrest) {
propertyConversionMethod = PropertyConversionMethod.SERIAL_NAME
} Without this, serial names get ignored when using this: Tardy::tardyDateTimes setTo tardyList?.plusElement(timeInstant) |
Beta Was this translation helpful? Give feedback.
Can you change this property when installing Postgrest:
Without this, serial names get ignored when using this:
Tardy::tardyDateTimes setTo tardyList?.plusElement(timeInstant)