Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Drizzle ORM · Function

uuid

uuid() defines a PostgreSQL UUID column in a Drizzle table schema. Combine it with defaultRandom() for database-generated UUIDs or default() for a fixed UUID value.

How it works

In Drizzle's PostgreSQL schema API, calling uuid() declares a column using PostgreSQL's UUID type. Add defaultRandom() to generate the column's default with gen_random_uuid(), or default() to provide a fixed UUID value.

Syntax

uuid()

Returns

A PostgreSQL UUID column builder.

Key points

  • uuid() represents PostgreSQL's UUID data type for universally unique identifiers.
  • PostgreSQL UUID columns are defined with the uuid and pgTable exports from drizzle-orm/pg-core.
  • Chain defaultRandom() when the database should create a random UUID automatically.
  • Use default() with a UUID string when a specific default value is required.
  • Drizzle emits a PostgreSQL gen_random_uuid() default for a UUID column configured with defaultRandom().

Common mistakes

  • Runtime default helpers generate values in Drizzle during inserts; this differs from a database default such as defaultRandom().
  • The column's UUID semantics follow PostgreSQL, so PostgreSQL-specific behavior may require consulting PostgreSQL documentation.

Example: PostgreSQL UUID column

import { pgTable, uuid } from "drizzle-orm/pg-core";

export const users = pgTable("users", {
  id: uuid().defaultRandom().primaryKey(),
});

Defines a PostgreSQL UUID column and configures the database to generate a random UUID when inserting a row.

When to use it

Define UUID columns

Use UUIDs as identifiers for PostgreSQL rows, especially when identifiers should not be sequential integers.

Generate random defaults

Let PostgreSQL assign an identifier automatically during insertion.

Set a fixed default

Provide a predetermined identifier value as the column default.

Interview questions

QWhat does uuid() declare?

uuid() creates a PostgreSQL UUID column in a pgTable definition. It maps to the database uuid type rather than a text or integer column.

Follow-up: How can the database generate identifiers automatically? Add defaultRandom(), which produces a gen_random_uuid() database default.

QHow do you configure UUID defaults?

defaultRandom() configures a random UUID database default. A literal UUID can instead be supplied with default('...'), producing a fixed SQL default.

Follow-up: What SQL default does defaultRandom() produce? It generates gen_random_uuid().

QHow is uuid used in a table schema?

The documented PostgreSQL form imports uuid and pgTable from drizzle-orm/pg-core. The schema can contain multiple UUID columns, each with its own default configuration.

Follow-up: How would you make the generated UUID the row identifier? Chain primaryKey() after defaultRandom().

  • pgTable
  • defaultRandom
  • default
  • primaryKey
  • drizzle-orm/pg-core

Source: PostgreSQL column types - Drizzle ORM. Checked 2026-10-16.