Background
This week, I focus on creating an initial schema for the project through using Prisma. This is for a MySQL database, and the PR for the initial schema is here.
This is an example of a model:
A model describes how a table would look like on a database.
Basics
- Record would be the table name.
- Green texts are the value types for the columns.
- The blue texts are the names of the columns.
-
@id
marks the primary key. -
?
marks a column as nullable. -
@default
sets the default value as what's in the parameter. -
now()
returns the current time. -
@updatedAt
would provide the value that's the time a row is updated. Foreign key:
In the example, the line withuser
is used to record a relationship between this and the other table, for the purpose of marking a foreign key.fields
's value is the foreign key, whilereferences
's value points to a column on another table.
On the other table, it need to have another corresponding line to fully establish the relationship, like:
[]
means it's a to-many relationship. You don't have that if it's to-one relationship.You might notice some odd type like
RecordType
. That's enumeration, and you can declare that in the same file:
As you can see, the green text is the name of the type, and the blue texts are the possible values for such type.
Experience
As far as creating schema goes, Prisma is very friendly and easy to use, once you learn the basic, such as how to mark primary key or make values in a column unique. Establishing foreign key and the relationship between tables take a bit more work to understand. It helps if you already know how a database works.
Because I need to create the database to serve the need of other parts of the project, such as what user information people who handle login want to store, I ask around the team. I sort of feel like someone who goes around with a clip board and ask what they would like to order for lunch. I ask them what they would like to include and why...to have better comprehension of the project and for my own learning, and it's always interesting to learn more.
Top comments (0)