Skip to content

Commit

Permalink
feat(multiSchema): Definition of relation table in multi schema cases…
Browse files Browse the repository at this point in the history
… + fixes (#4273)

Co-authored-by: Nilufar Bava <[email protected]>
  • Loading branch information
janpio and nilubava authored Nov 20, 2023
1 parent 0aa628c commit 7e04282
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -350,19 +350,23 @@ Implicit m-n relations:

If you obtain your data model from [introspection](/concepts/components/introspection), you can still use implicit m-n-relations by following Prisma's [conventions for relation tables](#conventions-for-relation-tables-in-implicit-m-n-relations). The following example assumes you want to create a relation table to get an implicit m-n-relation for two models called `Post` and `Category`.

##### Table names
##### Relation table

If you want a relation table to be picked up by introspection as an implicit m-n-relation, the name must follow this exact structure:

- It must start with an underscore `_`
- Then the name of the first table in alphabetical order (in this case `Category`)
- Then the name of the first model in alphabetical order (in this case `Category`)
- Then `To`
- Then the name of the second table in alphabetical order (in this case `Post`)
- Then the name of the second model in alphabetical order (in this case `Post`)

In the example, the correct table name is `_CategoryToPost`.

When creating an implicit m-n-relation yourself in the Prisma schema file, you can [configure the relation](#configuring-the-name-of-the-relation-table-in-implicit-many-to-many-relations) to have a different name. This will change the name given to the relation table in the database. For example, for a relation named `"MyRelation"` the corresponding table will be called `_MyRelation`.

###### Multi-schema

If your implicit many-to-many relationship spans multiple database schemas (using the [`multiSchema` preview feature](/guides/database/multi-schema)), the relation table (with the name defined directly above, in the example `_CategoryToPost`) must be present in the same database schema as the first model in alphabetical order (in this case `Category`).

##### Columns

A relation table for an implicit m-n-relation must have exactly two columns:
Expand Down

1 comment on commit 7e04282

@vercel
Copy link

@vercel vercel bot commented on 7e04282 Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./

docs-git-main-prisma.vercel.app
prisma2-docs.vercel.app
docs-prisma.vercel.app

Please sign in to comment.