I have two tables: Users and Tasks. Users has a on...
# orm-help
l
I have two tables: Users and Tasks. Users has a one-to-many relation with the Tasks table (meaning an user can have many tasks). I’m trying to create a task with a signed in user but I’m getting an
Copy code
Unique constraint failed on the fields: (`id`)
this is my service:
Copy code
async createTask(
    createTaskDto: Prisma.TaskCreateInput,
    user: User,
  ): Promise<Task> {
    return this.prismaService.task.create({
      data: {
        ...createTaskDto,
        user: {
          create: user,
        },
      },
    });
  }
and here is my db schema:
Copy code
model Task {
  id          String     @id @default(uuid())
  title       String     @db.VarChar(20)
  description String     @db.VarChar(200)
  status      TaskStatus @default(OPEN)
  user        User      @relation(fields: [userId], references: [id])
  userId      String
}

model User {
  id    String  @id @default(uuid())
  email String  @unique
  name  String?
  password String
  tasks Task[]
}
I’m don’t know what is wrong, maybe I’m not getting how the relations work in Prisma. Can someone help me?
1
I got it. I actually misunderstood how the relations works in prisma. I don’t need to “create” anything, just pass the user ID and when a query using
findMany
I have to pass the
ìncludes
option
👍 2