From 7d3587a2255721437ae4889129508df8928c0a55 Mon Sep 17 00:00:00 2001 From: Nikolas Burk Date: Mon, 24 Jun 2024 13:34:39 +0200 Subject: [PATCH] add docs for json default values --- .../100-prisma-schema/20-data-model/10-models.mdx | 5 ++++- .../100-working-with-json-fields.mdx | 12 ++++++++++++ .../500-reference/100-prisma-schema-reference.mdx | 2 +- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/content/200-orm/100-prisma-schema/20-data-model/10-models.mdx b/content/200-orm/100-prisma-schema/20-data-model/10-models.mdx index a000234ff2..27e93c0c38 100644 --- a/content/200-orm/100-prisma-schema/20-data-model/10-models.mdx +++ b/content/200-orm/100-prisma-schema/20-data-model/10-models.mdx @@ -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)): @@ -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]) @@ -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\" }")`. diff --git a/content/200-orm/200-prisma-client/200-special-fields-and-types/100-working-with-json-fields.mdx b/content/200-orm/200-prisma-client/200-special-fields-and-types/100-working-with-json-fields.mdx index b24e361f48..abebd66f94 100644 --- a/content/200-orm/200-prisma-client/200-special-fields-and-types/100-working-with-json-fields.mdx +++ b/content/200-orm/200-prisma-client/200-special-fields-and-types/100-working-with-json-fields.mdx @@ -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\" }") +} +``` \ No newline at end of file diff --git a/content/200-orm/500-reference/100-prisma-schema-reference.mdx b/content/200-orm/500-reference/100-prisma-schema-reference.mdx index f177f04f9f..1f6ddb05b4 100644 --- a/content/200-orm/500-reference/100-prisma-schema-reference.mdx +++ b/content/200-orm/500-reference/100-prisma-schema-reference.mdx @@ -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