drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]:set uuid to primarykey but can't set defaultRandom()

Open pbpoon opened this issue 2 years ago • 3 comments

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. (/Users/vincent_pun/WebStormProjects/restaurant/backend/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/index.cjs:40763:44) at Socket.emit (node:events:513:28) at addChunk (node:internal/streams/readable:324:12) at readableAddChunk (node:internal/streams/readable:297:9) at Readable.push (node:internal/streams/readable:234:10) at TCP.onStreamRead (node:internal/stream_base_commons:190:23) { length: 206, severity: 'ERROR', code: '42883', detail: undefined, hint: 'No function matches the given name and argument types. You might need to add explicit type casts.', position: undefined, internalPosition: undefined, internalQuery: undefined, where: undefined, schema: undefined, table: undefined, column: undefined, dataType: undefined, constraint: undefined, file: 'parse_func.c', line: '516', routine: 'ParseFuncOrColumn' }

Expected behavior

No response

Environment & setup

No response

pbpoon avatar Sep 16 '23 09:09 pbpoon

Don't you need an extension in postgres to make this work?

Angelelz avatar Dec 19 '23 20:12 Angelelz

@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()

Hebilicious avatar Jan 04 '24 16:01 Hebilicious

It would be pretty nice if drizzle was smart enough to add the CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

tcurdt avatar May 23 '24 21:05 tcurdt

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.

borisboskovic avatar Aug 26 '24 10:08 borisboskovic

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.

tcurdt avatar Aug 26 '24 10:08 tcurdt

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.

L-Mario564 avatar Oct 16 '24 18:10 L-Mario564