I am working on a workout app using Graphql Prisma and Postgres as my backend. So far my prisma schema is as follows:
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Exercise {
id Int @id @default(autoincrement())
name String
numSets Int
holdTime Int?
owner Workout? @relation(fields: [workoutId], references: [id])
workoutId Int?
}
model Workout {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
category String
exercises Exercise[]
}
The schema defines a Workout object that takes in a category and a list of Exercises. I am trying to create a workout with a list of objects using Prisma's nested create query however the list of exercises is not created. I can't tell if there is something wrong with my schema or my script.
The script that I am using to test out the create is:
const { PrismaClient } = require("@prisma/client")
const prisma = new PrismaClient()
async function main() {
const newWorkout = await prisma.workout.create({
data: {
category: 'Push',
exercises: {
create: [
{
name: "pushup",
numSets: 5
},
{
name: "headstand",
numSets: 3,
holdTime: 30
}
]
}
}
})
console.log(newWorkout)
const workout = await prisma.workout.findUnique({
where: {
id: 1
}
})
console.log(workout.exercises)
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
})
Output from print statements:
console.log(newWorkout): "{ id: 1, createdAt: 2021-11-09T07:03:40.844Z, updatedAt: 2021-11-09T07:03:40.848Z, category: 'Push' }"
console.log(workout.exercises): "undefined"