-
|
Similar to how serial('id') work, I was wondering if there is a way to create an id field with default value set by JS, something like the following: export const user = mysqlTable(
"User",
{
id: varchar("id", { length: 191 })
.primaryKey()
.default(crypto.randomUUID())
.notNull(),
name: varchar("name", { length: 191 }).notNull(),
...
},
(table) => {
return {
...
};
}
);Thanks for the help. |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 8 replies
-
|
It's not possible right now, but will be possible soon |
Beta Was this translation helpful? Give feedback.
-
|
+1 on this feature. |
Beta Was this translation helpful? Give feedback.
-
|
+1 |
Beta Was this translation helpful? Give feedback.
-
|
+1. something like how prisma does it with |
Beta Was this translation helpful? Give feedback.
-
|
Won't something like this work? |
Beta Was this translation helpful? Give feedback.
-
|
This has been implemented in v0.28.3 but you'll need to update to v.28.5. The new |
Beta Was this translation helpful? Give feedback.
-
|
as of latest version this works: id: uuid().primaryKey().defaultRandom(), |
Beta Was this translation helpful? Give feedback.
-
|
Hi, I built something similar on my website — you can check it out here:(https://uuidguidgenerator.com/) |
Beta Was this translation helpful? Give feedback.
-
This way it worked for me: import { v4 as uuidv4 } from 'uuid'
import { binary, mysqlTable, text, varchar } from 'drizzle-orm/mysql-core'
export const testTable = mysqlTable('test', {
id: varchar('id', { length: 16 })
.primaryKey()
.$default(() => uuidv4()),
id2: binary('id', { length: 16 })
.primaryKey()
.$default(() => uuidv4()),
id3: text('id')
.primaryKey()
.$default(() => uuidv4()),
}) |
Beta Was this translation helpful? Give feedback.



This has been implemented in v0.28.3 but you'll need to update to v.28.5. The new
.$defaultand.$defaultFnmethods lets you register a function that will be called on insert/update if not value is passed.