Out of the blue i'm getting this error, whllst it'...
# help
j
Out of the blue i'm getting this error, whllst it's working fine before. Any idea how I should fix this? I'm using autogenerated uuid v4 for my primary key
Copy code
code: "23505"
details: null
hint: null
message: "duplicate key value violates unique constraint \"watchlists_pkey\""
s
This would seem as if your uuid field isn't generating a unique uuid each time,
Did you create the table using the supabase dashboard or using SQL?
j
The dashboard. And used the function inside the default field
I’ll double check! In a couple mind and relay here.
@User just double checked in case I had a typo or anything but it's there
s
Yeah that looks correct
When do you get the error you are getting? what does the supabase-js code look like?
j
Copy code
const addProjectToWatchlist = async (userId, watchlistId, collectedArray) => {
    return await supabase
        .from('watchlists')
        .upsert({ id: watchlistId, author_id: userId, collected: collectedArray })
}
author_id
is a foreign key relation to the auth users database
collected
is a simple JSONB array
so
id
is the primary key, which i pass to the upsert method (The ID matches with the row inside the
watchlists
table and isn't a random ID.) Should this be
update
instead 🤔?
@User sorry, overall fairly new programmer and new to SQL. I guess I could use : https://supabase.io/docs/reference/javascript/upsert#upserting-into-tables-with-constraints here?
So that it becomes:
Copy code
const addProjectToWatchlist = async (userId, watchlistId, collectedArray) => {
    return await supabase
        .from('watchlists')
        .upsert({ id: watchlistId, author_id: userId, collected: collectedArray }, { onConflict: id })
}
Solved ✅ . Thanks for your help silentworks