From de9e561f883eb6ecfcf6fd779cb9f644500ad823 Mon Sep 17 00:00:00 2001 From: LIU HANCHENG Date: Thu, 23 Nov 2023 17:13:00 +0800 Subject: [PATCH] Update many-to-many relations doc (#5486) Co-authored-by: Nikolas --- .../300-many-to-many-relations.mdx | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/content/200-concepts/100-components/01-prisma-schema/06-relations/300-many-to-many-relations.mdx b/content/200-concepts/100-components/01-prisma-schema/06-relations/300-many-to-many-relations.mdx index c46d607cd1..362c31b8b4 100644 --- a/content/200-concepts/100-components/01-prisma-schema/06-relations/300-many-to-many-relations.mdx +++ b/content/200-concepts/100-components/01-prisma-schema/06-relations/300-many-to-many-relations.mdx @@ -356,7 +356,7 @@ If you want a relation table to be picked up by introspection as an implicit m-n - It must start with an underscore `_` - Then the name of the first model in alphabetical order (in this case `Category`) -- Then `To` +- Then the relationship (in this case `To`) - Then the name of the second model in alphabetical order (in this case `Post`) In the example, the correct table name is `_CategoryToPost`. @@ -413,6 +413,49 @@ CREATE TABLE "Post" ( ); ``` +And you can define multiple many-to-many relations between two tables by using the different relationship name. This example shows how the Prisma introspection works under such case: + +```sql +CREATE TABLE IF NOT EXISTS "User" ( + "id" SERIAL PRIMARY KEY +); +CREATE TABLE IF NOT EXISTS "Video" ( + "id" SERIAL PRIMARY KEY +); +CREATE TABLE IF NOT EXISTS "_UserLikedVideos" ( + "A" SERIAL NOT NULL, + "B" SERIAL NOT NULL, + CONSTRAINT "_UserLikedVideos_A_fkey" FOREIGN KEY ("A") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT "_UserLikedVideos_B_fkey" FOREIGN KEY ("B") REFERENCES "Video" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); +CREATE TABLE IF NOT EXISTS "_UserDislikedVideos" ( + "A" SERIAL NOT NULL, + "B" SERIAL NOT NULL, + CONSTRAINT "_UserDislikedVideos_A_fkey" FOREIGN KEY ("A") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT "_UserDislikedVideos_B_fkey" FOREIGN KEY ("B") REFERENCES "Video" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); +CREATE UNIQUE INDEX "_UserLikedVideos_AB_unique" ON "_UserLikedVideos"("A", "B"); +CREATE INDEX "_UserLikedVideos_B_index" ON "_UserLikedVideos"("B"); +CREATE UNIQUE INDEX "_UserDislikedVideos_AB_unique" ON "_UserDislikedVideos"("A", "B"); +CREATE INDEX "_UserDislikedVideos_B_index" ON "_UserDislikedVideos"("B"); +``` + +If you run `prisma db pull` on this database, the Prisma CLI will generate the following schema through introspection: + +```prisma +model User { + id Int @id @default(autoincrement()) + Video_UserDislikedVideos Video[] @relation("UserDislikedVideos") + Video_UserLikedVideos Video[] @relation("UserLikedVideos") +} + +model Video { + id Int @id @default(autoincrement()) + User_UserDislikedVideos User[] @relation("UserDislikedVideos") + User_UserLikedVideos User[] @relation("UserLikedVideos") +} +``` + #### Configuring the name of the relation table in implicit many-to-many relations When using Prisma Migrate, you can configure the name of the relation table that's managed by Prisma using the `@relation` attribute. For example, if you want the relation table to be called `_MyRelationTable` instead of the default name `_CategoryToPost`, you can specify it as follows: