MySQL UUID as PK

I am unable to get a PK setup which has a default UUID value. I have
logID: varchar('logID', { length: 48 }).default(sql`'UUID()'`).primaryKey(),
logID: varchar('logID', { length: 48 }).default(sql`'UUID()'`).primaryKey(),
in my table schema But it creates a migration query that literally adds the literal string 'UUID()' in the field
CREATE TABLE `feedLogs` (
`logID` varchar(48) NOT NULL DEFAULT 'UUID()',
`feedType` varchar(10),
CONSTRAINT `feedLogs_logID` PRIMARY KEY(`logID`)
);
CREATE TABLE `feedLogs` (
`logID` varchar(48) NOT NULL DEFAULT 'UUID()',
`feedType` varchar(10),
CONSTRAINT `feedLogs_logID` PRIMARY KEY(`logID`)
);
but running this in MySQL directly, the table is created with a PK that does default to an actual UUID string value
6 Replies
mr_pablo
mr_pabloOP16mo ago
Oh, and I'm using Drizzle ORM 0.28.2 and Kit 0.19.13
mr_pablo
mr_pabloOP16mo ago
I did
logID: varchar('logID', { length: 48 }).default(sql`UUID()`).primaryKey(),
logID: varchar('logID', { length: 48 }).default(sql`UUID()`).primaryKey(),
and
logID: varchar('logID', { length: 48 }).default(sql`'UUID()'`).primaryKey(),
logID: varchar('logID', { length: 48 }).default(sql`'UUID()'`).primaryKey(),
These don't work. MySQL has a UUID() method, but as mentioned, right now, despite using the magic sql operator, the literal string value of UUID() is being inserted, not an actual UUID
Angelelz
Angelelz16mo ago
You need to check the version of Mysql you are using. It may or may not support the use of non deterministic default values
Angelelz
Angelelz16mo ago
Stack Overflow
MySQL set default id UUID
I'm trying to create tables in a database that has an id field that will populate the id with an UUID by default. I tried something like: CREATE TABLE FOO ( id CHAR(36) PRIMARY KEY DEFAULT uuid...
Angelelz
Angelelz16mo ago
Maybe you can try it this way
logID: varchar('logID', { length: 48 }).default(sql`('UUID()')`).primaryKey(),
logID: varchar('logID', { length: 48 }).default(sql`('UUID()')`).primaryKey(),
Want results from more Discord servers?
Add your server