![]() Take an example where we are trying to fetch data that satisfies the query SELECT * FROM users WHERE 'email' = ' ' within a table that contains millions of rows of data (possibly operated by some ecommerce business). In cases where large data sets are involved, querying for data can be both a time-consuming and expensive activity, both on the infrastructure and business operations. How to configure indexes in Prismaīasic understanding and knowledge of Node.js, Git, and Prisma is a prerequisite for this tutorial. An avid techie, James loves contributing to open source and sharing his coding adventures in the form of intuitive technical content. When you define two relations between the same two models, you need to add the name argument in the attribute to disambiguate them.James Sinkala Follow James is a solution-oriented full-stack developer and passionate technical writer. ![]() Note: Implicit many-to-many relations in relational databases do not require the attribute. use a specific name for a relation table) you need to control how the relation table is represented in the underlying database (e.g.you define a many-to-many relation for MongoDB.the case when you have two relations between the same models) you need to disambiguate a relation (that's e.g.you define a one-to-one or one-to-many relation, it is required on one side of the relation (with the corresponding relation scalar field).The attribute can only be applied to the relation fields, not to scalar fields. Relation scalar field: authorId (relation field name + Id).Relation scalar naming conventionsīecause a relation scalar field always belongs to a relation field, the following naming convention is common: If you want to update a relation in your code, you can do so using nested writes. Relation scalar fields are read-only in the generated Prisma Client API. The "actual" field that represents the foreign key is required on that side of the relation as well, it's called relation scalar field, and is referenced inside attribute:Ī scalar field becomes a relation scalar field when it's used in the fields of a attribute. The side of the relation which is annotated with the attribute represents the side that stores the foreign key in the underlying database. many-to-many relations for MongoDB only.Relations that require one side of the relation to be annotated with the attribute are referred to as annotated relation fields. The other relation field called posts is defined purely on a Prisma-level, it doesn't manifest in the database. The relation scalar represents the foreign key in the underlying database. Relation fieldsīoth posts and author are relation fields because their types are not scalar types but other models.Īlso note that the annotated relation field author needs to link the relation scalar field authorId on the Post model inside the attribute. If you're not using Prisma Migrate but obtain your data model from introspection, you can still make use of implicit many-to-many relations by following Prisma's conventions for relation tables. If you use an implicit many-to-many relation instead of an explicit one, it makes the Prisma Client API simpler (because, for example, you have one fewer level of nesting inside of nested writes). However, Prisma manages this relation table. The implicit many-to-many relation still manifests in a relation table in the underlying database. You cannot use a in place of an use either of these features, you must set up an explicit many-to-many instead.Implicit many-to-many relations require both models to have a single Be aware of the following: implicit many-to-many relations, where Prisma manages the relation table and it does not appear in the Prisma schema.explicit many-to-many relations, where the relation table is represented as an explicit model in your Prisma schema.Many-to-many relations in relational databases can be modelled in two ways: Implicit and explicit many-to-many relations See the MongoDB section for more details. Notice that the syntax is slightly different between relational databases and MongoDB - particularly for many-to-many relations.įor relational databases, the following entity relationship diagram represents the database that corresponds to the sample Prisma schema:įor MongoDB, Prisma uses a normalized data model design, which means that documents reference each other by ID in a similar way to relational databases. These relations do not require the attribute unless you need to disambiguate relations. ![]() ![]() This example uses implicit many-to-many relations. This schema is the same as the example data model but has all scalar fields removed (except for the required relation scalars) so you can focus on the relation fields.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |