Prisma trpc schema multiple models

Hi guys I built a basic t3 stack app, and right now I am stuck. I am trying to create an additional model while creating another one. This is how my schema looks like:
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model Movie {
id String @id @default(cuid())
text String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
answer Answer? @relation(fields: [answerId], references: [id])
answerId String?
}

model Answer {
id String @id @default(cuid())
text String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Movie Movie[]
}
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model Movie {
id String @id @default(cuid())
text String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
answer Answer? @relation(fields: [answerId], references: [id])
answerId String?
}

model Answer {
id String @id @default(cuid())
text String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Movie Movie[]
}
And this is how I try to create the model
import { z } from "zod";

import { router, publicProcedure } from "../trpc";

export const movieRouter = router({
getAll: publicProcedure.query(({ ctx }) => {
return ctx.prisma.movie.findMany();
}),

create: publicProcedure.input(z.object({
text: z.string(),
answer: z.string(),
})).mutation(({ ctx, input }) => {

const answer = ctx.prisma.answer.create({
data: {
text: input.text,
}
});

return ctx.prisma.movie.create({
data: {
text: input.text,
answer: {
connect: {
id: answer.id,
}
},
// Connect the new Movie to the existing Answer using the 'movies' relation field
movies: {
connect: {
id: answer.id,
}
}
}
});
}),
});
import { z } from "zod";

import { router, publicProcedure } from "../trpc";

export const movieRouter = router({
getAll: publicProcedure.query(({ ctx }) => {
return ctx.prisma.movie.findMany();
}),

create: publicProcedure.input(z.object({
text: z.string(),
answer: z.string(),
})).mutation(({ ctx, input }) => {

const answer = ctx.prisma.answer.create({
data: {
text: input.text,
}
});

return ctx.prisma.movie.create({
data: {
text: input.text,
answer: {
connect: {
id: answer.id,
}
},
// Connect the new Movie to the existing Answer using the 'movies' relation field
movies: {
connect: {
id: answer.id,
}
}
}
});
}),
});
17 Replies
utdev
utdevOP•3y ago
While create a movie I am also trying to create an answer for that movie and connect them by relation. But right now here
...
connect: {
id: answer.id,
}
...
connect: {
id: answer.id,
}
I get this error:
Property 'id' does not exist on type 'Prisma__AnswerClient<Answer, never>'.ts(2339)
Property 'id' does not exist on type 'Prisma__AnswerClient<Answer, never>'.ts(2339)
This:
// Connect the new Movie to the existing Answer using the 'movies' relation field
movies: {
connect: {
id: answer.id,
}
}
// Connect the new Movie to the existing Answer using the 'movies' relation field
movies: {
connect: {
id: answer.id,
}
}
Also returns:
Type '{ text: string; answer: { connect: { id: any; }; }; movies: { connect: { id: any; }; }; }' is not assignable to type '(Without<MovieCreateInput, MovieUncheckedCreateInput> & MovieUncheckedCreateInput) | (Without<...> & MovieCreateInput)'.
Object literal may only specify known properties, and 'movies' does not exist in type 'Without<MovieUncheckedCreateInput, MovieCreateInput> & MovieCreateInput'.ts(2322)
Type '{ text: string; answer: { connect: { id: any; }; }; movies: { connect: { id: any; }; }; }' is not assignable to type '(Without<MovieCreateInput, MovieUncheckedCreateInput> & MovieUncheckedCreateInput) | (Without<...> & MovieCreateInput)'.
Object literal may only specify known properties, and 'movies' does not exist in type 'Without<MovieUncheckedCreateInput, MovieCreateInput> & MovieCreateInput'.ts(2322)
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
utdev
utdevOP•3y ago
yes While creating a movie in my movie's route mutation I try to create an answer and immidiatly connect their relation
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
utdev
utdevOP•3y ago
Hmm but that would not resolve my second mutation since I need the created answer.id right? So you mean something like this right?
const result = await Promise.all([
ctx.prisma.answer.create({
data: {
text: input.answer,
}
}),
ctx.prisma.movie.create({
data: {
text: input.movie,
answer: {
connect: {
id: answer.id, // NOT getting here the answer.id
}
},
movies: {
connect: {
id: answer.id,
}
}
}
});
])
const result = await Promise.all([
ctx.prisma.answer.create({
data: {
text: input.answer,
}
}),
ctx.prisma.movie.create({
data: {
text: input.movie,
answer: {
connect: {
id: answer.id, // NOT getting here the answer.id
}
},
movies: {
connect: {
id: answer.id,
}
}
}
});
])
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
utdev
utdevOP•3y ago
yes
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
utdev
utdevOP•3y ago
yeah but ctx.prisma.answer.create does not return a promise right, not sure if I am missing something 🤔
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
utdev
utdevOP•3y ago
Here
const answer = ctx.prisma.answer.create({
data: {
text: input.answer,
}
});
const answer = ctx.prisma.answer.create({
data: {
text: input.answer,
}
});
answer has this return type: const answer: Prisma.Prisma__AnswerClient<Answer, never>
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
utdev
utdevOP•3y ago
ok that works for the answer.id issue :D, I thought that it should work since default it is not async. Looks like this now
create: publicProcedure.input(z.object({
answer: z.string(),
movie: z.string(),
})).mutation(async ({ ctx, input }) => {

const answer = await ctx.prisma.answer.create({
data: {
text: input.answer,
}
});

return ctx.prisma.movie.create({
data: {
text: input.answer,
answer: {
connect: {
id: answer.id,
}
},
// Connect the new Movie to the existing Answer using the 'movies' relation field
movies: {
connect: {
id: answer.id,
}
}
}
});
create: publicProcedure.input(z.object({
answer: z.string(),
movie: z.string(),
})).mutation(async ({ ctx, input }) => {

const answer = await ctx.prisma.answer.create({
data: {
text: input.answer,
}
});

return ctx.prisma.movie.create({
data: {
text: input.answer,
answer: {
connect: {
id: answer.id,
}
},
// Connect the new Movie to the existing Answer using the 'movies' relation field
movies: {
connect: {
id: answer.id,
}
}
}
});
But the code below // Connect the new Movie to the existing Answer using the 'movies' relation field has still a warning Any idea how to resolve that? ohwait nevermind fixed that one works now I think thanks 😄
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
utdev
utdevOP•3y ago
Yeah what confused me is that .create did not return a promise default, so I just thought not to use it as a promise But now I know thanks 🙂
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
utdev
utdevOP•3y ago
ty
Want results from more Discord servers?
Add your server