Define UUID columns
Use UUIDs as identifiers for PostgreSQL rows, especially when identifiers should not be sequential integers.
Drizzle ORM · Function
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.
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.
uuid()A PostgreSQL UUID column builder.
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.
Use UUIDs as identifiers for PostgreSQL rows, especially when identifiers should not be sequential integers.
Let PostgreSQL assign an identifier automatically during insertion.
Provide a predetermined identifier value as the column default.
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.
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().
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().
pgTabledefaultRandomdefaultprimaryKeydrizzle-orm/pg-core