I need to generate an uuid that does not exist in a certain column in a table. I found a function gen_random_uuid()
. Is there some way in the loop to check if such an uuid exists and replace it if it does?
CREATE TABLE x (id UUID PRIMARY KEY, name TEXT)
INSERT INTO x VALUES (
gen_random_uuid(), // Need to check if uuid not exist in id column
'some name'
)
CodePudding user response:
There are more uuid than atoms in the universe. So the chance of randomly picking the same twice is virtually zero. That is exactly the point of uuid, you do not have to check that they are unique. You just assume they are by construction.