hey! I'm using Prisma for a school project and in ...
# orm-help
p
hey! I'm using Prisma for a school project and in some older projects, really loving it so far. I was wondering how I would achieve something like that :
Copy code
model Classroom {
  id String @id @default(cuid())

  type     Int      @default(0)
  name     String

  teachers Teacher[] // when a teacher is added, append to the teacher's "classes" attribute
}

model Teacher {
  id String @id @default(cuid())

  user   User   @relation(fields: [userId], references: [id], onDelete: Cascade)
  userId String @unique
  
  classes Classroom[] // when a class is added, add the teacher to the matching class' "teachers" attribute

  subjects Subject[]
}
b
try something like this :
Copy code
const addClassroom = await prisma.Classroom.create({
    data:{
        type: yourtipe,
        teachers:idTeacher
    }})
and make same for addTeacher with the classId
n
Here’s an example query which creates a teacher and a classroom and connects them.
Copy code
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient({
  log: ['query'],
});

async function main() {
  // Create Teacher,User and Class
  await prisma.teacher.create({
    data: {
      user: {
        create: {
          name: 'John Doe',
        },
      },
      classes: {
        create: {
          name: 'Math',
        },
      },
    },
  });
}

main()
  .catch((e) => {
    throw e;
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
p
I'll try this out in a bit of time, is the teacher in the `Classroom`'s
teachers
too ?