Is there any way I could check if a name exists in...
# sql
d
Is there any way I could check if a name exists in
name
column of my table? I'd like to throw an error if it does.
s
It sounds like you'd probably need a nested query
d
@User no I need the UID of user as primary key
k
So, you can do constrains where u check if name exists or not. Or u can use id and name as primary
U can have two primary keys :)
d
Hmmm interesting.... I'm looking for what you mean by constraints
k
You can have a boolean function in ur constraints that validate the user name. For example Ur function return not exists select name from users where name = param_name
d
Copy code
js
await supabase
  .from('users')
  .select("*")
  .eq('name', 'MyName')
I could do this and check if name exists but I'm looking if there's a way that'll throw an error if exists w/o primary key 😅 New to it so I'd like the default primary key
Like my query above?
k
Yeah, use constraints.
Constraints are db level.
d
Sorry for noob question but what exactly here is a constraint? The
.eq()
?
k
Exactly what u looking for and what I menat
Meant*
d
I get you now
How do you uses
not exists
in JS SDK ?
k
Here u r validating using client side which is not sufficient. By using constraints u are making sure no one can go around and use the api to alter ur client side validation:)
d
Yes yes I;m doing this on Express server
k
neq is what u need.
d
let me try
but at the end it's 2 step process right? Because I don't think neq can be used with insert
This links is very helpful :)
Yeah, that's why I suggested to use either multiple primary keys or constraints on the table itself.
When u do the constraints, the insertion will fail.
And u don't need to worry about checking and doing several reads to see if it exists or not.
d
Copy code
js
const {data} = await supabase.from("users").select("name").eq("name", "MyName")

if (data.length) {
  // name taken
}
not sure if this is okay to do ?
k
It is okay definitely, but postgres is flexible enough to make sure we avoid redundant queries like that. But if that fits u, then go with it, but when u scale, this is gonna be costy..
d
I get the constraint thing somewhat now but how do you use that in JS SDK?
I can't see that in docs
k
Its not on server side, its on the table itself. The constraints can be done using query on the table. Follow the link from stack overflow I shared.
d
Okay thanks
oh it's so hard too understand that SQL syntax 😦
I can't find supabase js way so it's easy to understand at first place
k
it is, at first, but believe me, once u figure out functions in supabase and postgres, you going to be so creative and u going to end up writing less code 🙂
s
It's confusing at first, but it gets easier. Learning it opens up a lot more options for querying your data than you could realistically achieve using the JS lib. I'd argue that it's easier than using sequelize (an ORM) in some aspects. To put perspective on that, the last time I wrote SQL queries was back in 2014 and those were only basic ones. It took me a week or 2 while being with Supabase to be able to fluently write intermediate queries, and another few weeks to be able to write fluent queries that performed well. It's a good skill to know.
d
Cool, I'm trying to relate SQL with Mongo somewhat... like Projections are select n so on..