model User {
id Int @id @default(autoincrement())
profile Profile?
}
model Profile {
id Int @id @default(autoincrement())
user User @relation(fields: [userId], references: [id])
userId Int @unique // relation scalar field (used in the `@relation` attribute above)
}
model User {
id Int @id @default(autoincrement())
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
author User @relation(fields: [authorId], references: [id])
authorId Int
}
model Post {
id Int @id @default(autoincrement())
author User @relation(fields: [authorId], references: [id] , onDelete: Cascade) // 追加
authorId Int
}
model User {
id Int @id @default(autoincrement())
}
model Post {
id Int @id @default(autoincrement())
}
model User {
id Int @id @default(autoincrement())
posts Post[] // ● ← 追記
}
model Post {
id Int @id @default(autoincrement())
}
model User {
id Int @id @default(autoincrement())
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
user User @relation(fields: [userId], references: [id]) // ● 自動
userId Int // ● 自動
}
あとは フィールド名やデータベースの物理カラム名など書き加えます。
const user = await this.prisma.user.findUnique({
where: { email: email },
include: { posts: true },
})
const user = await this.prisma.user.findUnique({
where: { email: email },
select: {
id: true,
email: true,
posts: {
select:{
id: true,
}
}
},
})