Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
//add-next-line

generator client {
provider = "prisma-client-js"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ CREATE TABLE public."Post" (

After introspecting your database with `npx prisma db pull`, you will have a new `Post` model in your `schema.prisma` file:

```prisma file=schema.prisma
```prisma file=schema.prisma showLineNumbers
model Post {
id BigInt @id
title String @db.String(200)
Expand All @@ -81,7 +81,7 @@ When generating unique identifiers for records in a distributed database like Co

Instead, Prisma ORM provides the [`autoincrement()`](/orm/reference/prisma-schema-reference#autoincrement) attribute function, which uses CockroachDB's [`unique_rowid()` function](https://www.cockroachlabs.com/docs/stable/serial.html) for generating unique identifiers. For example, the following `User` model has an `id` primary key, generated using the `autoincrement()` function:

```prisma file=schema.prisma
```prisma file=schema.prisma showLineNumbers
model User {
id BigInt @id @default(autoincrement())
name String
Expand All @@ -96,7 +96,7 @@ For more information on generating database keys, see CockroachDB's [Primary key

To connect to a CockroachDB database server, you need to configure a [`datasource`](/orm/prisma-schema/overview/data-sources) block in your [Prisma schema file](/orm/prisma-schema):

```prisma file=schema.prisma
```prisma file=schema.prisma showLineNumbers
datasource db {
provider = "cockroachdb"
url = env("DATABASE_URL")
Expand Down Expand Up @@ -204,7 +204,7 @@ When introspecting a CockroachDB database, the database types are mapped to Pris

[Introspection](/orm/prisma-schema/introspection) adds native database types that are **not yet supported** as [`Unsupported`](/orm/reference/prisma-schema-reference#unsupported) fields:

```prisma file=schema.prisma
```prisma file=schema.prisma showLineNumbers
model Device {
id BigInt @id @default(autoincrement())
interval Unsupported("INTERVAL")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ You can then use Prisma ORM and define relations in your Prisma schema without t

In that case, you can define a relation as with other database that supports foreign key constraints, for example:

```prisma file=schema.prisma
```prisma file=schema.prisma showLineNumbers
model Post {
id Int @id @default(autoincrement())
title String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ If you do not specify a referential action, Prisma ORM [uses a default](#referen

</TopBlock>

<Admonition type="alert">
<Admonition type="danger">

If you upgrade from a version earlier than 2.26.0:
It is extremely important that you check the [upgrade paths for referential actions](/orm/more/upgrade-guides/upgrading-versions/upgrading-to-prisma-3/referential-actions) section. Prisma ORM's support of referential actions **removes the safety net in Prisma Client that prevents cascading deletes at runtime**. If you use the feature _without upgrading your database_, the [old default action](/orm/more/upgrade-guides/upgrading-versions/upgrading-to-prisma-3/referential-actions#prisma-orm-2x-default-referential-actions) - `ON DELETE CASCADE` - becomes active. This might result in cascading deletes that you did not expect.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ CREATE TABLE "User" (
);

-- AddForeignKey
//highlight-start
ALTER TABLE "Post"
ADD CONSTRAINT "Post_authorId_fkey"
FOREIGN KEY ("authorId")
REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
//highlight-end
```

In this case, the foreign key constraint on the `authorId` column of the `Post` table references the `id` column of the `User` table, and guarantees that a post must have an author that exists. If you update or delete a user then the `ON DELETE` and `ON UPDATE` referential actions specify the `CASCADE` option, which will also delete or update all posts belonging to the user.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ const getAuthor = await prisma.user.findUnique({
id: "20",
},
include: {
//highlight-next-line
| posts: true, // All posts where authorId == 20
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ You can optionally use the `map` argument to explicitly define the **underlying

When introspecting a database, the `map` argument will _only_ be rendered in the schema if the name _differs_ from Prisma ORM's [default constraint naming convention for indexes and constraints](#prisma-orms-default-naming-conventions-for-indexes-and-constraints).

<Admonition type="alert">
<Admonition type="danger">

If you use Prisma Migrate in a version earlier than 2.29.0 and want to maintain your existing constraint and index names after upgrading to a newer version, **do not** immediately run `prisma migrate` or `prisma db push`. This will **change any underlying constraint name that does not follow Prisma ORM's convention**. Follow the [upgrade path that allows you to maintain existing constraint and index names](/orm/more/upgrade-guides/upgrading-versions/upgrading-to-prisma-3/named-constraints#option-1-i-want-to-maintain-my-existing-constraint-and-index-names).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,12 +475,14 @@ const result = await prisma.user.create({
data: {
email: 'elsa@prisma.io',
name: 'Elsa Prisma',
//highlight-start
posts: {
create: [
{ title: 'How to make an omelette' },
{ title: 'How to eat an omelette' },
],
},
//highlight-end
},
include: {
posts: true, // Include all posts in the returned object
Expand Down Expand Up @@ -559,6 +561,7 @@ const result = await prisma.user.create({
data: {
email: 'yvette@prisma.io',
name: 'Yvette',
//highlight-start
posts: {
create: [
{
Expand All @@ -572,6 +575,7 @@ const result = await prisma.user.create({
{ title: 'How to eat an omelette' },
],
},
//highlight-end
},
include: {
// Include posts
Expand Down Expand Up @@ -645,11 +649,13 @@ The example uses a nested `include` to include all posts.
const result = await prisma.user.create({
data: {
email: 'saanvi@prisma.io',
//highlight-start
posts: {
createMany: {
data: [{ title: 'My first post' }, { title: 'My second post' }],
},
},
//highlight-end
},
include: {
posts: true,
Expand Down Expand Up @@ -743,9 +749,11 @@ The following query creates ([`create`](/orm/reference/prisma-client-reference#c
const result = await prisma.user.create({
data: {
email: 'vlad@prisma.io',
//highlight-start
posts: {
connect: [{ id: 8 }, { id: 9 }, { id: 10 }],
},
//highlight-end
},
include: {
posts: true, // Include all posts in the returned object
Expand Down Expand Up @@ -793,10 +801,12 @@ const result = await prisma.user.update({
id: 9,
},
data: {
//highlight-start
posts: {
connect: {
id: 11,
},
//highlight-end
},
},
include: {
Expand All @@ -819,6 +829,7 @@ If a related record may or may not already exist, use [`connectOrCreate`](/orm/r
const result = await prisma.post.create({
data: {
title: 'How to make croissants',
//highlight-start
author: {
connectOrCreate: {
where: {
Expand All @@ -830,6 +841,7 @@ const result = await prisma.post.create({
},
},
},
//highlight-end
},
include: {
author: true,
Expand Down Expand Up @@ -875,9 +887,11 @@ const result = await prisma.user.update({
id: 16,
},
data: {
//highlight-start
posts: {
disconnect: [{ id: 12 }, { id: 19 }],
},
//highlight-end
},
include: {
posts: true,
Expand Down Expand Up @@ -914,9 +928,11 @@ const result = await prisma.post.update({
id: 23,
},
data: {
//highlight-start
author: {
disconnect: true,
},
//highlight-end
},
include: {
author: true,
Expand Down Expand Up @@ -956,9 +972,11 @@ const result = await prisma.user.update({
id: 16,
},
data: {
//highlight-start
posts: {
set: [],
},
//highlight-end
},
include: {
posts: true,
Expand Down Expand Up @@ -994,9 +1012,11 @@ const result = await prisma.user.update({
id: 11,
},
data: {
//highlight-start
posts: {
deleteMany: {},
},
//highlight-end
},
include: {
posts: true,
Expand All @@ -1014,11 +1034,13 @@ const result = await prisma.user.update({
id: 11,
},
data: {
//highlight-start
posts: {
deleteMany: {
published: false,
},
},
//highlight-end
},
include: {
posts: true,
Expand All @@ -1034,9 +1056,11 @@ const result = await prisma.user.update({
id: 6,
},
data: {
//highlight-start
posts: {
deleteMany: [{ id: 7 }],
},
//highlight-end
},
include: {
posts: true,
Expand All @@ -1054,6 +1078,7 @@ const result = await prisma.user.update({
id: 6,
},
data: {
//highlight-start
posts: {
updateMany: {
where: {
Expand All @@ -1064,6 +1089,7 @@ const result = await prisma.user.update({
},
},
},
//highlight-end
},
include: {
posts: true,
Expand All @@ -1079,6 +1105,7 @@ const result = await prisma.user.update({
id: 6,
},
data: {
//highlight-start
posts: {
update: {
where: {
Expand All @@ -1089,6 +1116,7 @@ const result = await prisma.user.update({
},
},
},
//highlight-end
},
include: {
posts: true,
Expand All @@ -1106,6 +1134,7 @@ const result = await prisma.post.update({
id: 6,
},
data: {
//highlight-start
author: {
upsert: {
create: {
Expand All @@ -1118,6 +1147,7 @@ const result = await prisma.post.update({
},
},
},
//highlight-end
},
include: {
author: true,
Expand All @@ -1135,11 +1165,13 @@ const result = await prisma.user.update({
id: 9,
},
data: {
//highlight-start
posts: {
createMany: {
data: [{ title: 'My first post' }, { title: 'My second post' }],
},
},
//highlight-end
},
include: {
posts: true,
Expand Down Expand Up @@ -1169,6 +1201,7 @@ For example, the following query returns `User` that meet the following criteria
```ts highlight=3-14;normal
const users = await prisma.user.findMany({
where: {
//highlight-start
posts: {
none: {
views: {
Expand All @@ -1181,6 +1214,7 @@ const users = await prisma.user.findMany({
},
},
},
//highlight-end
},
include: {
posts: true,
Expand All @@ -1200,6 +1234,7 @@ For example, the following query returns `Post` records that meet the following
```ts highlight=3-13;normal
const users = await prisma.post.findMany({
where: {
//highlight-start
author: {
isNot: {
name: 'Bob',
Expand All @@ -1211,6 +1246,7 @@ const users = await prisma.post.findMany({
},
},
},
//highlight-end
include: {
author: true,
},
Expand All @@ -1224,9 +1260,11 @@ For example, the following query uses `none` to return all users that have zero
```ts highlight=3-5;normal
const usersWithZeroPosts = await prisma.user.findMany({
where: {
//highlight-start
posts: {
none: {},
},
//highlight-end
},
include: {
posts: true,
Expand All @@ -1241,6 +1279,7 @@ The following query returns all posts that don't have an author relation:
```js highlight=3;normal
const postsWithNoAuthor = await prisma.post.findMany({
where: {
//highlight-next-line
author: null, // or author: { }
},
include: {
Expand All @@ -1256,9 +1295,11 @@ The following query returns all users with at least one post:
```ts highlight=3-5;normal
const usersWithSomePosts = await prisma.user.findMany({
where: {
//highlight-start
posts: {
some: {},
},
//highlight-end
},
include: {
posts: true,
Expand Down
Loading