Need help with creating a mutation that involves relations

I'm a complete beginner just started and I'm really confused on how I will create this router with the mutation with all the relations The schema is pretty big so I'll show one relation:
model Profile {
id String @id @default(cuid())
experiences Experience[]

}

model Experience {
id String @id @default(cuid())
companyName String
Profile Profile? @relation(fields: [profileId], references: [id])
profileId String?
}
model Profile {
id String @id @default(cuid())
experiences Experience[]

}

model Experience {
id String @id @default(cuid())
companyName String
Profile Profile? @relation(fields: [profileId], references: [id])
profileId String?
}
So the inputed array could be with multiple "experiences" data : { experiences: [ {.... experience 1}, {... experience 2}, ... etc
], } And I want to create a mutation that can take that entire data and input it into the profile model, and this is my attempt on it, but i have no idea on how to make it actually work
import { z } from "zod";
import {
createTRPCRouter,
protectedProcedure,
publicProcedure,
} from "@/server/api/trpc";

export const profileRouter = createTRPCRouter({
createProfile: protectedProcedure
.input(z.object({

experiences: z.array(z.object({
companyName: z.string(),

})),

}))
.mutation(async ({ ctx, input }) => {
return ctx.db.profile.create({
data: {
experiences: {
ctx.db.experiences.create(input.experiences),
},
},
});
}),
});
import { z } from "zod";
import {
createTRPCRouter,
protectedProcedure,
publicProcedure,
} from "@/server/api/trpc";

export const profileRouter = createTRPCRouter({
createProfile: protectedProcedure
.input(z.object({

experiences: z.array(z.object({
companyName: z.string(),

})),

}))
.mutation(async ({ ctx, input }) => {
return ctx.db.profile.create({
data: {
experiences: {
ctx.db.experiences.create(input.experiences),
},
},
});
}),
});
3 Replies
Viszy A
Viszy AOP12mo ago
the image is a similar attempt but still am running into errors
Vargas
Vargas12mo ago
I see you are running input.map.project, input.projects would be the right variable
Viszy A
Viszy AOP12mo ago
yeah i solved it thanks
Want results from more Discord servers?
Add your server