Swift: Upsert data
- Primary keys must be included in
values
to use upsert.
Examples
Upsert your data
struct Instrument: Encodable \{
let id: Int
let name: String
\}
try await supabase.database
.from("instruments")
.upsert(Instrument(id: 1, name: "piano"))
.execute()
Bulk Upsert your data
struct Instrument: Encodable \{
let id: Int
let name: String
\}
try await supabase.database
.from("instruments")
.upsert([
Instrument(id: 1, name: "piano"),
Instrument(id: 2, name: "guitar"),
])
.execute()
Upserting into tables with constraints
struct User: Encodable \{
let id: Int
let handle: String
let displayName: String
enum CodingKeys: String, CodingKey \{
case id
case handle
case displayName = "display_name"
\}
\}
try await supabase.database
.from("users")
.upsert(
User(id: 42, handle: "saoirse", displayName: "Saoirse"),
onConflict: "handle"
)
.execute()