Skip to content

Commit

Permalink
document id generation methods (#6474)
Browse files Browse the repository at this point in the history
* document id generation methods

* Update content/200-orm/500-reference/100-prisma-schema-reference.mdx

* Update content/200-orm/500-reference/100-prisma-schema-reference.mdx

* Update content/200-orm/500-reference/100-prisma-schema-reference.mdx

---------

Co-authored-by: Ankur Datta <[email protected]>
  • Loading branch information
nikolasburk and ankur-arch authored Dec 4, 2024
1 parent c6335d4 commit 1490035
Showing 1 changed file with 112 additions and 15 deletions.
127 changes: 112 additions & 15 deletions content/200-orm/500-reference/100-prisma-schema-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1600,7 +1600,11 @@ Defines a [default value for a field](/orm/prisma-schema/data-model/models#defin
- [`sequence()`](#sequence) (CockroachDB only)
- [`dbgenerated(...)`](#dbgenerated)
- [`cuid()`](#cuid)
- [`cuid(2)`](#cuid)
- [`uuid()`](#uuid)
- [`uuid(4)`](#uuid)
- [`uuid(7)`](#uuid)
- [`nanoid()`](#nanoid)
- [`now()`](#now)
- Default values that cannot yet be represented in the Prisma schema are represented by the [`dbgenerated(...)` function](#dbgenerated) when you use [introspection](/orm/prisma-schema/introspection).
- Default values are not allowed on relation fields in the Prisma schema. Note however that you can still define default values on the fields backing a relation (the ones listed in the `fields` argument in the `@relation` attribute). A default value on the field backing a relation will mean that relation is populated automatically for you.
Expand Down Expand Up @@ -3017,15 +3021,14 @@ model User {

Generate a globally unique identifier based on the [`cuid`](https://github.com/ericelliott/cuid) spec.

If you'd like to use [`cuid2`](https://github.com/paralleldrive/cuid2) values, you can pass `2` as an argument to the `cuid` function: `cuid(2)`.

#### Remarks

- Compatible with `String`
- Compatible with `String`.
- Implemented by Prisma ORM and therefore not "visible" in the underlying database schema. You can still use `cuid()` when using [introspection](/orm/prisma-schema/introspection) by [manually changing your Prisma schema](/orm/prisma-client/setup-and-configuration/custom-model-and-field-names) and [generating Prisma Client](/orm/prisma-client/setup-and-configuration/generating-prisma-client), in that case the values will be generated by Prisma's [query engine](/orm/more/under-the-hood/engines).
- Since the length of `cuid()` output is undefined per the cuid creator, a safe field size is 30 characters, in order to allow for enough characters for very large values. If you set the field size as less than 30, and then a larger value is generated by `cuid()`, you might see Prisma Client errors such as `Error: The provided value for the column is too long for the column's type.`

##### MongoDB

- `cuid()` does not generate a valid `ObjectId` - [use the `@db.ObjectId` syntax](#generate-objectid-as-ids-mongodb-only) if you want to use `ObjectId` in the underlying database. However, you can still use `cuid()` if your `_id` field is not of type `ObjectId`.
- For **MongoDB**: `cuid()` does not generate a valid `ObjectId`. You can use [`@db.ObjectId` syntax](#generate-objectid-as-ids-mongodb-only) if you want to use `ObjectId` in the underlying database. However, you can still use `cuid()` if your `_id` field is not of type `ObjectId`.

#### Examples

Expand Down Expand Up @@ -3054,35 +3057,127 @@ model User {
</TabItem>
</TabbedContent>

##### Generate `cuid(2)` values as IDs based on the `cuid2` spec

<TabbedContent code>
<TabItem value="Relational databases">

```prisma
model User {
id String @id @default(cuid(2))
name String
}
```

</TabItem>
<TabItem value="MongoDB">

```prisma
model User {
id String @id @default(cuid(2)) @map("_id")
name String
}
```

</TabItem>
</TabbedContent>

### `uuid()`

Generate a globally unique identifier based on the [UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier) spec, version 4 (random).
Generate a globally unique identifier based on the [UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier) spec. Prisma ORM supports versions 4 (default) and 7.

#### Remarks

- Compatible with `String`
- Compatible with `String`.
- Implemented by Prisma ORM and therefore not "visible" in the underlying database schema. You can still use `uuid()` when using [introspection](/orm/prisma-schema/introspection) by [manually changing your Prisma schema](/orm/prisma-client/setup-and-configuration/custom-model-and-field-names) and [generating Prisma Client](/orm/prisma-client/setup-and-configuration/generating-prisma-client), in that case the values will be generated by Prisma ORM's [query engine](/orm/more/under-the-hood/engines).
- For **relational databases**: If you do not want to use Prisma ORM's `uuid()` function, you can use [the native database function with `dbgenerated`](#override-default-value-behavior-for-supported-types).
- For **MongoDB**: `uuid()` does not generate a valid `ObjectId`. You can use [`@db.ObjectId` syntax](#generate-objectid-as-ids-mongodb-only) if you want to use `ObjectId` in the underlying database. However, you can still use `uuid()` if your `_id` field is not of type `ObjectId`.

<Admonition type='info'>
#### Examples

**Note (Relational databases)**: If you do not want to use Prisma ORM's `uuid()` function, you can use [the native database function with `dbgenerated`](#override-default-value-behavior-for-supported-types).
##### Generate `uuid()` values as IDs using UUID v4

</Admonition>
<TabbedContent code>
<TabItem value="Relational databases">

##### MongoDB
```prisma
model User {
id String @id @default(uuid())
name String
}
```

- `uuid()` does not generate a valid `ObjectId` - [use the `@db.ObjectId` syntax](#generate-objectid-as-ids-mongodb-only) if you want to use `ObjectId` in the underlying database. However, you can still use `uuid()` if your `_id` field is not of type `ObjectId`.
</TabItem>
<TabItem value="MongoDB">

```prisma
model User {
id String @id @default(uuid()) @map("_id")
name String
}
```

</TabItem>
</TabbedContent>

##### Generate `uuid(7)` values as IDs using UUID v7

<TabbedContent code>
<TabItem value="Relational databases">

```prisma
model User {
id String @id @default(uuid(7))
name String
}
```

</TabItem>
<TabItem value="MongoDB">

```prisma
model User {
id String @id @default(uuid(7)) @map("_id")
name String
}
```

</TabItem>
</TabbedContent>

### `nanoid()`

Generated values based on the [Nano ID](https://github.com/ai/nanoid) spec.

:::info

Nano ID is quite comparable to UUID v4 (random-based). It has a similar number of random bits in the ID (126 in Nano ID and 122 in UUID), so it has a similar collision probability:

For there to be a one in a billion chance of duplication, 103 trillion version 4 IDs must be generated.

There are two main differences between Nano ID and UUID v4:

- Nano ID uses a bigger alphabet, so a similar number of random bits are packed in just 21 symbols instead of 36.
- Nano ID code is 4 times smaller than uuid/v4 package: 130 bytes instead of 423.

:::

#### Remarks

- Compatible with `String`.
- Implemented by Prisma ORM and therefore not "visible" in the underlying database schema. You can still use `uuid()` when using [introspection](/orm/prisma-schema/introspection) by [manually changing your Prisma schema](/orm/prisma-client/setup-and-configuration/custom-model-and-field-names) and [generating Prisma Client](/orm/prisma-client/setup-and-configuration/generating-prisma-client), in that case the values will be generated by Prisma ORM's [query engine](/orm/more/under-the-hood/engines).
- For **MongoDB**: `nanoid()` does not generate a valid `ObjectId`. You can use [`@db.ObjectId` syntax](#generate-objectid-as-ids-mongodb-only) if you want to use `ObjectId` in the underlying database. However, you can still use `nanoid()` if your `_id` field is not of type `ObjectId`.

#### Examples

##### Generate `uuid()` values as IDs
##### Generate `nanoid()` values as IDs

<TabbedContent code>
<TabItem value="Relational databases">

```prisma
model User {
id String @id @default(uuid())
id String @id @default(nanoid())
name String
}
```
Expand All @@ -3092,14 +3187,16 @@ model User {

```prisma
model User {
id String @id @default(uuid()) @map("_id")
id String @id @default(nanoid()) @map("_id")
name String
}
```

</TabItem>
</TabbedContent>



### `now()`

Set a timestamp of the time when a record is created.
Expand Down

0 comments on commit 1490035

Please sign in to comment.