Android Question Supabase - Upsert support

PoppaBart

Member
Does anyone know if the supabase.b4x library has the ability to implement the upsert command. Otherwise is there a way to write a onconflict -> replace routine?
 

Alexander Stolte

Expert
Licensed User
Longtime User
Does anyone know if the supabase.b4x library has the ability to implement the upsert command.
Yes with the upsert command:
B4X:
    Dim Insert As Supabase_DatabaseInsert = xSupabase.Database.InsertData
    Insert.From("dt_Tasks")
    Insert.Upsert 'Upserting is an operation that performs both: Inserting a new row if a matching row doesn't already exist. Either updating the existing row, or doing nothing, if a matching row already exists.
    Dim InsertMap As Map = CreateMap("Tasks_Name":"Task 07","Tasks_Checked":False,"Tasks_CreatedAt":DateUtils.TicksToString(DateTime.Now),"Tasks_UpdatedAt":DateUtils.TicksToString(DateTime.Now))
    Wait For (Insert.Insert(InsertMap).Upsert.Execute) Complete (Result As SupabaseError)
 
Upvote 0

PoppaBart

Member
Yes with the upsert command:
B4X:
    Dim Insert As Supabase_DatabaseInsert = xSupabase.Database.InsertData
    Insert.From("dt_Tasks")
    Insert.Upsert 'Upserting is an operation that performs both: Inserting a new row if a matching row doesn't already exist. Either updating the existing row, or doing nothing, if a matching row already exists.
    Dim InsertMap As Map = CreateMap("Tasks_Name":"Task 07","Tasks_Checked":False,"Tasks_CreatedAt":DateUtils.TicksToString(DateTime.Now),"Tasks_UpdatedAt":DateUtils.TicksToString(DateTime.Now))
    Wait For (Insert.Insert(InsertMap).Upsert.Execute) Complete (Result As SupabaseError)
Thanks, my bad I didn't read the functions list you provided close enough.
 
Upvote 0

PoppaBart

Member
But I have never tested whether it works, so feedback is welcome
Alexander,

Does not look like it works. I was using Wait For (insert.Insert(InsertMap).Execute) Complete (Result As SupabaseError), I added an existing item to the table and received

ResponseError. Reason: , Response: {"code":"23505","details":"Key (\"Item\")=(Steak) already exists.","hint":null,"message":"duplicate key value violates unique constraint \"GroceryItems_Item_key\""}


I then added the insert.Upsert and Wait For (insert.Insert(InsertMap).Upsert.Execute) Complete (Result As SupabaseError), again I added an existing item to the table and received the same error.

Hope this helps
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Thanks for the feedback. I had another look at the JavaScript and Flutter version and they still set an "OnConflict" parameter. It will take me some time to find a solution for this as there is only limited documentation of the API.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
I then added the insert.Upsert and Wait For (insert.Insert(InsertMap).Upsert.Execute) Complete (Result As SupabaseError), again I added an existing item to the table and received the same error.
I have just tested it and unfortunately I have to tell you that it works for me. If I omit the upsert, I get your error message and as soon as I add the upsert, it updates the value in the database.

That's my code:
B4X:
    Dim Insert As Supabase_DatabaseInsert = xSupabase.Database.InsertData
    Insert.From("users")
    Insert.Upsert
    Dim InsertMap As Map = CreateMap("id":"492422e5-4188-4b40-9324-fee7c46be527","username":"Alex")
    Wait For (Insert.Insert(InsertMap).Execute) Complete (Result As SupabaseError)
 
Upvote 0

PoppaBart

Member
I have just tested it and unfortunately I have to tell you that it works for me. If I omit the upsert, I get your error message and as soon as I add the upsert, it updates the value in the database.

That's my code:
B4X:
    Dim Insert As Supabase_DatabaseInsert = xSupabase.Database.InsertData
    Insert.From("users")
    Insert.Upsert
    Dim InsertMap As Map = CreateMap("id":"492422e5-4188-4b40-9324-fee7c46be527","username":"Alex")
    Wait For (Insert.Insert(InsertMap).Execute) Complete (Result As SupabaseError)
Thanks, I have now managed to get it to work. I didn't set the CreateMap parameters correctly.
 
Upvote 0
Top