
Join the Conversation!
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
In the previous lesson, we learned how to create a single in Prisma, which corresponds to a in our database. Now, let's explore how to create multiple models and establish relationships between them. Creating Multiple Models
To create multiple models, we simply define them one after another in our file. Let's say we want to create a model in addition to our model:
model User {
id Int @id @default(autoincrement())
name String
email String @unique
posts Post[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Post {
id Int @id @default(autoincrement())
title String
content String
author User @relation(fields: [authorId], references: [id])
authorId Int
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
Here, we've defined two models: and . Each model has its own set of fields, similar to what we learned in the previous lesson.
In the above example, we've also established a relationship between the and models. Let's break it down: In the model:
posts Post[]
This line specifies that a can have multiple associated with it. The notation indicates a one-to-many relationship. Think of this line as saying that one can have an array (many)
In order to the complete the relation between and , we need to define the relationship from the side as well. This is done using the attribute.
In the model:
author User @relation(fields: [authorId], references: [id])
authorId Int
These lines define the relationship from the side.
The field is of type , indicating that each is associated with a single .
The attribute specifies the details of the relationship. It indicates that the field in the model corresponds to the field in the model.
The Generated SQL When we run Prisma's generate command, it will create the corresponding SQL tables based on our model definitions:
model User {
id Int @id @default(autoincrement())
name String
email String @unique
posts Post[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Post {
id Int @id @default(autoincrement())
title String
content String
author User @relation(fields: [authorId], references: [id])
authorId Int
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
);
CREATE TABLE Post (
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(191) NOT NULL,
content TEXT NOT NULL,
authorId INTEGER NOT NULL,
createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (authorId) REFERENCES User(id)
);
As you can see, Prisma generates the appropriate SQL statements to create the and tables, along with the necessary foreign key constraint to establish the relationship between them. By leveraging Prisma's declarative syntax and powerful code generation capabilities, we can easily define and manage multiple tables and their relationships in our database. In the next lesson, we'll explore how to perform database operations using Prisma's intuitive and type-safe API.
"Please login to view comments"
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.