P
Prisma•2w ago
Keith#1141

Clarification on Prisma and DB Congruence

Dear Prisma Support Staff, I hope this message finds you well! I am a Software Engineering student currently using Prisma and have some clarifications to make. I have read in the documentation that the Prisma Schema is the single source of truth and it can either be pushed or migrated to the backend to construct the backend, or if the backend has already constructed Prisma may introspect the backend to construct the schema. Thus, it is my understanding that the Prisma schema follows a similar configuration to the database records. This goes against information that has also be asserted upon me. I had been told that the Prisma models and the database should not be similar and that Prisma should retrieve the records from the db and reconstruct them again to match their own models. Here is an example: DB Table 1 has a username field DB Table 2 has a password field The backend retrieves both and reconstructs them together to match the Prisma object - User. This interferes with my understanding of Prisma migration/introspection which ensures that the schema and db are kept in sync. Thank you for your assistance in the matter and I look forward to your speedy reply! Sincerely, Keith Software Engineering Student
3 Replies
Nurul
Nurul•2w ago
Hello 👋
I had been told that the Prisma models and the database should not be similar and that Prisma should retrieve the records from the db and reconstruct them again to match their own models.
Prisma models should match the structure of your database tables. I think it's incorrect to say that Prisma models and database should not be similar. Prisma schema can serve as the single source of truth for your application's data model. There are two ways through which you can ensure that your Prisma models (schema.prisma file) and your database are in sync. 1. Migration: You manually define your data model in the Prisma schema and then use Prisma Migrate to map this schema to your database npx prisma migrate dev command https://www.prisma.io/docs/orm/prisma-migrate/getting-started 2. Introspection: You can use introspection to generate the Prisma schema from the current database schema. npx prisma db pull command https://www.prisma.io/docs/orm/prisma-schema/introspection
Getting started with Prisma Migrate | Prisma Documentation
Learn how to migrate your schema in a development environment using Prisma Migrate.
What is introspection? (Reference) | Prisma Documentation
Learn how you can introspect your database to generate a data model into your Prisma schema.
Keith#1141
Keith#1141•2w ago
Thank you very much for the response sir @Nurul! It is deeply appreciated! One of the arguments made against the Prisma models and the database being in sync is that Prisma is an ORM and, in the term ORM, M stands for Mapper. I was told that this suggests that Prisma models should not be similar to the database it should just MAP to it, hence, this example: DB Table 1 has a username field DB Table 2 has a password field The backend retrieves both and reconstructs them together to match the Prisma object - User. ^I was informed that this is proper mapping and this is what should be applied. Which given my understanding and your response is wrong. May I ask for clarification on this mapper term too sir? Thank you again for the response! Sincerely, Keith Software Engineering Student Sorry sir @Nurul if my responses seem redundant it has been a huge point of contestation in my class and would severely affect my ability to move forward with my education. I genuinely appreciate your response and assistance!
Eli S
Eli S•2w ago
If the data is in separate tables, I'd say the models will be separate even if they are related (models model a table, not the data in your application, but can be combined in more complex types when related query elements are pulled in with an include). Say you have a User and a Password table. The User will probably have a relation to a Password id. When the User is queried with an {include: {passwordRelation: true}}, you may have some object on the queried User like {id: 20, username: "X", passwordRelation: {id:19, userId: 20, passwordhash: "somehash"}} Then the type would be User & {passwordRelation: Password}. All things in one table's rows should relate to one external key. @Keith#1141 Replace table with collection if you're using something like MongoDb. But basically the models model the structure in the database not in the application. The ability to reference different keys via programmatic methods (like include: {xyz:true}} is a feature of an ORM like Prisma, so you don't have to perform selects/finds on matching table's id in a different query or a subquery.
Want results from more Discord servers?
Add your server