Use a specific convention for relation tablesĭo not require the attribute unless you need to disambiguate relations with a name, e.g. } ) Rules for defining an implicit m-n relation Creates a new Category that is associated with the newly created Post record.Creates a new record in the relation table CategoriesOnPosts.You can query the relation model directly ( prisma.categoriesOnPosts(.)), or use nested queries to go from Post -> CategoriesOnPosts -> Category or the other way. The following section demonstrates how to query an explicit m-n-relation. ![]() If you're not using Prisma Migrate but obtain your data model from introspection, you can still make use of implicit m-n-relations by following Prisma's conventions for relation tables. When you don't need to attach additional information to the relation, you can model m-n-relations as implicit m-n-relations. Note that the same rules as for 1-n relations apply (because Post↔ CategoriesOnPosts and Category ↔ CategoriesOnPosts are both in fact 1-n relations), which means one side of the relation needs to be annotated with the attribute. In this example, the model representing the relation table also defines additional fields that describe the Post/ Category relationship - who assigned the category ( assignedBy), and when the category was assigned ( assignedAt):ĪLTER TABLE "CategoriesOnPosts" ADD CONSTRAINT "CategoriesOnPosts_categoryId_fkey" FOREIGN KEY ( "categoryId" ) REFERENCES "Category" ( "id" ) ON DELETE RESTRICT ON UPDATE CASCADE The relation table CategoriesOnPosts connects related Post and Category records. The fields of a relation table model are both annotated relation fields ( post and category) with a corresponding relation scalar field ( postId and categoryId). One model that represents the relation table, such as CategoriesOnPosts (also sometimes called JOIN, link or pivot table) in the underlying database.Two models with m-n relation, such as Category and Post.Explicit m-n relations define three models: In an explicit m-n relation, the relation table is represented as a model in the Prisma schema and can be used in queries. You can always migrate to an explicit m-n-relation later if needed. We recommend using implicit m-n-relations if you do not need to store any additional meta-data in the relation table itself. ![]() m-n-relations can be either explicit or implicit in the Prisma schema. In relational databases, m-n-relations are typically modelled via relation tables. Prisma schema syntax and the implementation in the underlying database differs between relational databases and MongoDB.
0 Comments
Leave a Reply. |