-
Notifications
You must be signed in to change notification settings - Fork 988
Revises multiple locations describing compound IDs and unique constraints #4591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a092904
Removes existing sections on compound ID in Prisma Client and adds a …
sabinadams b541275
Updates wording and adds clarifying message to as well
sabinadams cb3abcd
identifier -> constraint
sabinadams 93f6e11
Merge branch 'main' into compound-id-unique-id-section
sabinadams 8b67fde
Merge branch 'main' into compound-id-unique-id-section
nikolasburk e39a7a8
Update content/200-concepts/100-components/01-prisma-schema/04-data-m…
nikolasburk 8f779e0
Update content/200-concepts/100-components/01-prisma-schema/04-data-m…
nikolasburk b408b6e
Update content/200-concepts/100-components/02-prisma-client/051-worki…
nikolasburk 3c4fcf5
Update content/200-concepts/100-components/02-prisma-client/051-worki…
nikolasburk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
...ient/051-working-with-fields/300-working-with-composite-ids-and-constraints.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| --- | ||
| title: 'Working with compound IDs and unique constraints' | ||
| metaTitle: 'Working with compound IDs and unique constraints (Concepts)' | ||
| metaDescription: 'How to read, write, and filter by compound IDs and unique constraints.' | ||
| tocDepth: 2 | ||
| --- | ||
|
|
||
| <TopBlock> | ||
|
|
||
| Composite IDs and compound unique constraints can be defined in your Prisma schema using the [`@@id`](/reference/api-reference/prisma-schema-reference#id-1) <span class="api"></span> and [`@@unique`](/reference/api-reference/prisma-schema-reference#unique-1) <span class="api"></span> attributes. | ||
|
|
||
| <Admonition type="warning"> | ||
|
|
||
| **MongoDB does not support `@@id`**<br /> | ||
| MongoDB does not support composite IDs, which means you cannot identify a model with a `@@id` attribute. | ||
|
|
||
| </Admonition> | ||
|
|
||
| A composite ID or compound unique constraint uses the combined values of two fields as a primary key or identifier in your database table. In the following example, the `postId` field and `userId` field are used as a composite ID for a `Like` table: | ||
|
|
||
| ```prisma highlight=22;normal | ||
| model User { | ||
| id Int @id @default(autoincrement()) | ||
| name String | ||
| post Post[] | ||
| likes Like[] | ||
| } | ||
|
|
||
| model Post { | ||
| id Int @id @default(autoincrement()) | ||
| content String | ||
| User User? @relation(fields: [userId], references: [id]) | ||
| userId Int? | ||
| likes Like[] | ||
| } | ||
|
|
||
| model Like { | ||
| postId Int | ||
| userId Int | ||
| User User @relation(fields: [userId], references: [id]) | ||
| Post Post @relation(fields: [postId], references: [id]) | ||
|
|
||
| @@id([postId, userId]) | ||
| } | ||
| ``` | ||
|
|
||
| Querying for records from the `Like` table (e.g. using `prisma.like.findMany()`) would return objects that look as follows: | ||
|
|
||
| ```json | ||
| { | ||
| "postId": 1, | ||
| "userId": 1 | ||
| } | ||
| ``` | ||
|
|
||
| Although there are only two fields in the response, those two fields make up a compound ID named `postId_userId`. | ||
|
|
||
| You can also create a named compound ID or compound unique constraint by using the `@@id` or `@@unique` attributes' `name` field. For example: | ||
|
|
||
| ```prisma highlight=7;normal | ||
| model Like { | ||
| postId Int | ||
| userId Int | ||
| User User @relation(fields: [userId], references: [id]) | ||
| Post Post @relation(fields: [postId], references: [id]) | ||
|
|
||
| @@id(name: "likeId", [postId, userId]) | ||
| } | ||
| ``` | ||
|
|
||
| </TopBlock> | ||
|
|
||
| ## Where you can use compound IDs and unique constraints | ||
|
|
||
| Compound IDs and compound unique constraints can be used when working with _unique_ data. | ||
|
|
||
| Below is a list of Prisma Client functions that accept a compound ID or compound unique constraint in the `where` filter of the query: | ||
|
|
||
| - `findUnique` | ||
| - `findUniqueOrThrow` | ||
| - `delete` | ||
| - `update` | ||
| - `upsert` | ||
|
|
||
| A composite ID and a composite unique constraint is also usable when creating relational data with `connect` and `connectOrCreate`. | ||
|
|
||
| ## Filtering records by a compound ID or unique constraint | ||
|
|
||
| Although your query results will not display a compound ID or unique constraint as a field, you can use these compound values to filter your queries for unique records: | ||
|
|
||
| ```ts highlight=3-6;normal | ||
| const like = await prisma.like.findUnique({ | ||
| where: { | ||
| likeId: { | ||
| userId: 1, | ||
| postId: 1, | ||
| }, | ||
| }, | ||
| }) | ||
| ``` | ||
|
|
||
| <Admonition type="info"> | ||
|
|
||
| Note composite ID and compound unique constraint keys are only available as filter options for _unique_ queries such as `findUnique` and `findUniqueOrThrow`. See the [section](/concepts/components/prisma-client/working-with-fields/working-with-composite-ids/#where-you-can-use-compound-ids-and-unique-identifiers) above for a list of places these fields may be used. | ||
|
|
||
| </Admonition> | ||
|
|
||
| ## Deleting records by a compound ID or unique constraint | ||
|
|
||
| A compound ID or compound unique constraint may be used in the `where` filter of a `delete` query: | ||
|
|
||
| ```ts highlight=3-6;normal | ||
| const like = await prisma.like.delete({ | ||
| where: { | ||
| likeId: { | ||
| userId: 1, | ||
| postId: 1, | ||
| }, | ||
| }, | ||
| }) | ||
| ``` | ||
|
|
||
| ## Updating and upserting records by a compound ID or unique constraint | ||
|
|
||
| A compound ID or compound unique constraint may be used in the `where` filter of an `update` query: | ||
|
|
||
| ```ts highlight=3-6;normal | ||
| const like = await prisma.like.update({ | ||
| where: { | ||
| likeId: { | ||
| userId: 1, | ||
| postId: 1, | ||
| }, | ||
| }, | ||
| data: { | ||
| postId: 2, | ||
| }, | ||
| }) | ||
| ``` | ||
|
|
||
| They may also be used in the `where` filter of an `upsert` query: | ||
|
|
||
| ```ts highlight=3-6;normal | ||
| await prisma.like.upsert({ | ||
| where: { | ||
| likeId: { | ||
| userId: 1, | ||
| postId: 1, | ||
| }, | ||
| }, | ||
| update: { | ||
| userId: 2, | ||
| }, | ||
| create: { | ||
| userId: 2, | ||
| postId: 1, | ||
| }, | ||
| }) | ||
| ``` | ||
|
|
||
| ## Filtering relation queries by a compound ID or unique constraint | ||
|
|
||
| Compound IDs and compound unique constraint can also be used in the `connect` and `connectOrCreate` keys used when connecting records to create a relationship. | ||
|
|
||
| For example, consider this query: | ||
|
|
||
| ```ts highlight=6-9;normal | ||
| await prisma.user.create({ | ||
| data: { | ||
| name: 'Alice', | ||
| likes: { | ||
| connect: { | ||
| likeId: { | ||
| postId: 1, | ||
| userId: 2, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| ``` | ||
|
|
||
| The `likeId` compound ID is used as the identifier in the `connect` object that is used to locate the `Like` table's record that will be linked to the new user: `"Alice"`. | ||
|
|
||
| Similarly, the `likeId` can be used in `connectOrCreate`'s `where` filter to attempt to locate an existing record in the `Like` table: | ||
|
|
||
| ```ts highlight=10-13;normal | ||
| await prisma.user.create({ | ||
| data: { | ||
| name: 'Alice', | ||
| likes: { | ||
| connectOrCreate: { | ||
| create: { | ||
| postId: 1, | ||
| }, | ||
| where: { | ||
| likeId: { | ||
| postId: 1, | ||
| userId: 1, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.