Generated primary keys
Assign generated identifiers without passing an ID from application code.
Prisma · Const
@default defines the value Prisma or the database uses when a scalar field is omitted during creation. It accepts literals, supported functions, and database-generated expressions.
Place @default on a scalar field to supply its value when a create operation omits that field. The value can be a literal, a Prisma-provided function such as now(), cuid(), or uuid(), or a database-generated expression; relation fields themselves cannot receive defaults.
@default(_ value: Expression, map: String?)| Name | Type | Description |
|---|---|---|
value | Expression | Default expression, such as a literal, function, or database-generated value. |
map | String? | Optional mapped database default name; SQL Server only. |
A default value for the annotated scalar field.
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
active Boolean @default(true)
}This model assigns an automatically generated integer ID, the creation timestamp, and an initial Boolean value when records are created.
Assign generated identifiers without passing an ID from application code.
Set creation timestamps automatically when records are inserted.
Initialize flags, counters, JSON values, or scalar lists with consistent defaults.
@default supplies a value when a scalar field is not provided during creation. It may represent a database DEFAULT or use a Prisma ORM-level function, depending on the expression.
Follow-up: How would you model a default on a relation? Use the scalar field listed in the relation's fields argument; relation fields themselves cannot have defaults.
Use literals such as false or quoted JSON, and functions such as now(), cuid(), uuid(), and autoincrement() where supported. Prisma-provided cuid() and uuid() are generated by Prisma Client's query engine for all connectors.
Follow-up: What is required for a JSON object default? Quote the entire JSON value and escape each internal double quote with a backslash.
For relational databases, a default can become a database DEFAULT value, while some functions are evaluated by Prisma ORM. This distinction affects where the value is generated.
Follow-up: How are unsupported database defaults represented after introspection? Prisma uses dbgenerated() for defaults that cannot yet be expressed in the schema.
@id@relationautoincrement()cuid()uuid()now()dbgenerated()