Skip to content

Commit

Permalink
add docs for json default values
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolasburk committed Jun 24, 2024
1 parent 0b46df1 commit 7d3587a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ Fields of type `Unsupported` are not available in the generated Prisma Client AP
## Defining attributes

Attributes modify the behavior of fields or model blocks. The following example includes three field attributes ([`@id`](/orm/reference/prisma-schema-reference#id) , [`@default`](/orm/reference/prisma-schema-reference#default) , and [`@unique`](/orm/reference/prisma-schema-reference#unique) ) and one block attribute ([`@@unique`](/orm/reference/prisma-schema-reference#unique-1) ):
Attributes modify the behavior of fields or model blocks. The following example includes three field attributes ([`@id`](/orm/reference/prisma-schema-reference#id) , [`@default`](/orm/reference/prisma-schema-reference#default) , and [`@unique`](/orm/reference/prisma-schema-reference#unique) ) and one block attribute ([`@@unique`](/orm/reference/prisma-schema-reference#unique-1)):

<TabbedContent code>
<TabItem value="Relational databases">
Expand Down Expand Up @@ -761,6 +761,8 @@ model Post {
title String
//highlight-next-line
published Boolean @default(false)
//highlight-next-line
data Json @default("{ \"hello\": \"world\" }")
author User @relation(fields: [authorId], references: [id])
authorId Int
categories Category[] @relation(references: [id])
Expand Down Expand Up @@ -795,6 +797,7 @@ Default values can be:
- Static values that correspond to the field type, such as `5` (`Int`), `Hello` (`String`), or `false` (`Boolean`)
- [Lists](/orm/reference/prisma-schema-reference#-modifier) of static values, such as `[5, 6, 8]` (`Int[]`) or `["Hello", "Goodbye"]` (`String`[]). These are available in Prisma ORM versions `4.0.0` and later, when using supported databases (PostgreSQL, CockroachDB and MongoDB)
- [Functions](#using-functions), such as [`now()`](/orm/reference/prisma-schema-reference#now) or [`uuid()`](/orm/reference/prisma-schema-reference#uuid)
- JSON data which needs to be enclosed with double-quotes inside the `@default` attribute, e.g.: `@default("[]")`. If you want to provide a JSON object, you need to escape its double quotes using a backslash, e.g.: `@default("{ \"hello\": \"world\" }")`.

<Admonition type="info">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1000,3 +1000,15 @@ No - it is not yet possible to filter on the presence of a specific key.
### Is case insensitive filtering supported?

No - [case insensitive filtering](https://github.com/prisma/prisma/issues/7390) is not yet supported.

### How to set a default value for JSON fields?

When you want to set a `@default` value the `Json` type, you need to enclose it with double-quotes inside the `@default` attribute, for example:

```prisma
model User {
id Int @id @default(autoincrement())
json1 Json @default("[]")
json2 Json @default("{ \"hello\": \"world\" }")
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -1605,10 +1605,10 @@ Defines a [default value for a field](/orm/prisma-schema/data-model/models#defin
- [`cuid()`](#cuid)
- [`uuid()`](#uuid)
- [`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.
- Default values can be used with [scalar lists](/orm/prisma-client/special-fields-and-types/working-with-scalar-lists-arrays) in databases that natively support them.
- JSON data which needs to be enclosed with double-quotes inside the `@default` attribute, e.g.: `@default("[]")`. If you want to provide a JSON object, you need to escape its double quotes using a backslash, e.g.: `@default("{ \"hello\": \"world\" }")`.

##### MongoDB

Expand Down

0 comments on commit 7d3587a

Please sign in to comment.