add a prefix to UUID?

I am wondering if there is a functionality where I can add a custom prefix the uuid's. These uuids would be used as a pirmary key in my case. The end result would be something like this: product_9B4F49D6F35C49FEA0dCC579A8ED4755
Sillvva
Sillvva17d ago
Is the idea is to distinguish between ids from different tables?
xxxxx
xxxxx17d ago
yes and for the users of api to understand what the id is pointing to
Sillvva
Sillvva17d ago
You can use crypo.randomUUID() to generate a UUID in side drizzle's .defaultFn() method like this:
id: text('id').primaryKey().$defaultFn(() => `product_${crypto.randomUUID()}`)
id: text('id').primaryKey().$defaultFn(() => `product_${crypto.randomUUID()}`)
xxxxx
xxxxx17d ago
thank you so much :D
Sillvva
Sillvva17d ago
This is an application-level default, not a db-level default. If you tried to insert in an SQL client, the default would not be applied.
xxxxx
xxxxx17d ago
yes there probably wont be any operations made using sql client
iolyd
iolyd16d ago
For a schema level implementation, you could probably get it working with something along the lines of
const products = pgTable('products', {
id: text('id').primaryKey().default(sql`CONCAT('product_', REPLACE(uuid_generate_v4()::text, '-', '' ))`)
})
const products = pgTable('products', {
id: text('id').primaryKey().default(sql`CONCAT('product_', REPLACE(uuid_generate_v4()::text, '-', '' ))`)
})
Want results from more Discord servers?
Add your server
More Posts
[BUG?] Handling Postgresql schemas when using enums"drizzle-orm": "^0.30.10" Hello. I have a simple table with a status column ```ts export const BaseSHelp needed for Nested QueryFor this example, I have 4 tables - `user`, `podcast`, `episodes`, and `bookmarks`. - bookmarks tablHow to init sqlite project?https://github.com/drizzle-team/drizzle-orm/issues/2240 ?Typescript performance in larger codebases?Hey, I've been using drizzle with small projects, but now I'm thinking of migrating a larger codebasmultiple dbs, multiple drizzle client, not registering as multiple dbsHey all, anyone know why apiDb shows up as not working? Originally when I had `keys` schema importedSetting triggers with drizzleHey, good day guys. I'm a web developer who recently picked up mysql and drizzle. Going through the Multiple Where ClausesI need to filter my data based on various filters but only if they are not null. How can I do this, Enum as primary key for roles (sqlite)I'm pretty new to Drizzle and database schemas in general so bear with me if this is stupid. I'm crHow can I get a better structured response when querying a table (many to many)?Hi, I am new to sql. This might be obvious, but I'm not sure how to go about this. Below is a simplisql where column md5Hey, how can I achieve the following statement in drizzle? ```sql -- mysql SELECT * FROM podcasts_Error on Query Filter: Types of property 'table' are incompatible.I'm currently trying to filter a query as [explained in on the docs](https://orm.drizzle.team/docs/rHow to do Computed Fields / Manipulate data at Query Level?I'm looking for something like this, on prisma we can validate fields when querying the model. I coupgEnum conversion not working as expectedI have the following pgEnum defined: ``` export const ApprovalRequestTypePgEnum = pgEnum('approval_Filtering a jsonb with the shape Array<{id:string,name:string}>Hi. I have a query that returns all the posts with it's categories like this, ```typescript type PosCan't update table columnI update the column's name in my schema, but it don't update when I check in Drizzle Studio. AnyoneRunning an insert script, and I'm getting some "warnings".How do you access the warning details or what rows they relate to?