drizzle-orm
drizzle-orm copied to clipboard
[BUG]:set uuid to primarykey but can't set defaultRandom()
What version of drizzle-orm are you using?
0.28.6
What version of drizzle-kit are you using?
0.19.13
Describe the Bug
set uuid to primarykey but can't set defaultRandom()
export const address = pgTable('address', {
id: uuid('id').primaryKey().defaultRandom(),
name: varchar('name').notNull(),
phone: varchar('phone', { length: 12 }).notNull(),
address: text('address').notNull(),
createdAt: timestamp('created_at').defaultNow(),
updatedAt: timestamp('updated_at').default(sqlCURRENT_TIMESTAMP),
user_id: uuid('user_id')
.references(() => user.id)
.notNull(),
});
drizzle-kit: drizzle-kit push:pg --config=drizzle.config.ts
drizzle-kit: v0.19.13 drizzle-orm: v0.28.6
Custom config path was provided, using 'drizzle.config.ts'
Reading config file '/Users/vincent_pun/WebStormProjects/restaurant/backend/drizzle.config.ts'
error: function gen_random_uuid() does not exist
at Parser.parseErrorMessage (/Users/vincent_pun/WebStormProjects/restaurant/backend/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/index.cjs:40722:98)
at Parser.handlePacket (/Users/vincent_pun/WebStormProjects/restaurant/backend/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/index.cjs:40563:25)
at Parser.parse (/Users/vincent_pun/WebStormProjects/restaurant/backend/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/index.cjs:40487:34)
at Socket.
Expected behavior
No response
Environment & setup
No response
Don't you need an extension in postgres to make this work?
@Angelelz Correct
If anyone wants to use uuids with Postgres, you need to manually add the extension uuid-ossp to the db :
-- Create Extensions for UUIDs
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
Then with drizzle you can use :
const uuid = sql`uuid_generate_v4()`
// In a table definition ...
id: text('id').primaryKey().default(uuid).notNull()
It would be pretty nice if drizzle was smart enough to add the CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
It would be pretty nice if drizzle was smart enough to add the
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
The user you're querying the database with might not have the privilege to create extensions.
The user you're querying the database with might not have the privilege to create extensions.
There is alway the option to make the generation optional.
If they can create and drop tables I am not sure why one would avoid that privilege (for the most part). It just seems a little odd one has to do this manually for 80% of the cases.
The create extension support isn't being worked on right now. For now, we'll make it clear in the documentation that adding the uuid-ossp extension is required to use .defaultRandom.