Swift: Upsert data
- Primary keys must be included in
values
to use upsert.
Examples
Upsert your data
struct Country: Encodable \{
let id: Int
let name: String
\}
try await supabase.database
.from("countries")
.upsert(Country(id: 1, name: "Albania"))
.execute()
Bulk Upsert your data
struct Country: Encodable \{
let id: Int
let name: String
\}
try await supabase.database
.from("countries")
.upsert([
Country(id: 1, name: "Albania"),
Country(id: 2, name: "Algeria"),
])
.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()