
Join the Conversation!
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
What is a Prisma ? In Prisma, our "models" are our that we learned about in the lessons. These are written by us in the file. Just like , these models will define the structure of the data we hold in our Database.
Models are defined directly in the file, right below our and declarations.
To create a table, we'd define a model like this:
schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_URL")
}
model User {
id Int @id @default(autoincrement())
name String
email String @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
If you think back to what you learned in the lessons, you might be able to figure out what each section does already. Let's break it down!
The first line defined the name of our , in this case: "User".
model User {
...
}
Everything inside of this declaration will be our and .
Let's look at the first line of this .
id Int @id @default(autoincrement())
Just like with , we can break this up into sections.
: Our column's name
: Our column's type, this id column will hold integer (number) values
: Our column's clauses and constraints
The clause specifies that this column is the for our table. This means that each value in this column must be unique, and it will be used to identify each record in our table.
The clause tells Prisma that if no value is provided for this column when creating a new record, it should automatically generate a unique, incrementing integer value for it.
Next, let's look at the name and email columns:
name String
email String @unique
For these columns, we have:
and : The column names
: The column type. This specifies that the name and email columns will hold string values.
: This is a constraint applied to the email column. It ensures that each value in the email column must be unique across all records in the table.
Finally, let's look at the createdAt and updatedAt columns:
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Here, we have:
and : The column names
: The column type. This specifies that these columns will hold date and time values.
: This clause tells Prisma to set the value of createdAt to the current date and time whenever a new record is created, if no value is provided.
: This clause tells Prisma to automatically update the updatedAt column with the current date and time whenever a record is updated.
All of the ideas here should feel like review. You've already learned how tables work from the perspective, so understanding what is doing should make sense.
When we write in our file, we're telling prisma to generate the for us. Prisma will take the generate SQL and create these tables for us in our database.
model User {
id Int @id @default(autoincrement())
name String
email String @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
CREATE TABLE User (
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(191) NOT NULL,
email VARCHAR(191) NOT NULL,
createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)
As you can see already- the code we write for is simple yet powerful. In the next lesson we'll go over generating models with and connecting tables together.
"Please login to view comments"
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.