How can u make a default value be a unique string....
# help
j
How can u make a default value be a unique string. I see timestamps u can put now() can i have something for default unique usernames?
n
Hello @jar! This thread has been automatically created from your message in #843999948717555735 a ``few seconds ago``. Pinging @User so that they see this as well! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ... menu) and select Leave Thread to unsubscribe from future updates. Want to change the title? Use the
/title
command! We have solved your problem? Click the button below to archive it.
g
Can you explain or give an example of what you see the unique string looking like?
n
jar (2022-04-11)
j
Perhaps like how github or some of these things give like random combination of words like green-slick-turtle or something. Although honestly this is my first time really playing with supabase/postgresql so idk all the sql language stuff but may take the time to get good. Im guessing for that would need another table where I predefine a large set of words and randomly get 3 from and check if that combo exists already and if not try again. I just used uuid_generate_v4() and might be fine for now. Also when u select varchar why can I not type in the length?
g
Well you can probably find an algorithm out there that would generate a random combo, but I doubt it is guaranteed to be unique. To insure it is unique you would have to select on the table to see if the combo was used before and regen if not. Does not seem practical. BUT to attempt to do such a thing you would use a trigger on before insert to the table, call a trigger function and do your magic name generation in the function and then set new.name to your value. When the row is inserted it would have your value. Varchar length is really only useful if you are trying to enforce a max length in Postgres, it does not save space or time in general.
j
Oh ok. If i do want to force max length can i define it somewhere? Idk why even offers text and varchar if cant define length on it. Like usernames should have max length like 30 or something. So for the trigger i made it so it triggers on created user so i guess I can do it in there. So I set username field as non-null and force-unique and if nothing is chosen for username it would fail which makes sense now. Ok I think u put me on path to solution though. Ive been using aws amplify but theres something attractive about supabase to me in its clean simplicity I want to give it a shot
g
I'd get your stuff going then worry about varchar length or you can always set a constraint on length for text or varchar. You can also alter the table to change the column to varchar(30). I honestly don't know if the table ui supports it or not.
j
Ok Ill keep playing around with it. Thanks for your help