Farrukh Faizy
Farrukh Faizy

Reputation: 1235

Explicit many to many relation prisma

I have these 3 prisma models,

model user {
  id                        String                 @id @default(cuid())
  createdAt                 DateTime               @default(now())
  updatedAt                 DateTime               @default(now())
  courses                   userOnCourse[]
}

model course {
  id                      String                         @id @default(cuid())
  createdAt               DateTime                       @default(now())
  updatedAt               DateTime                       @default(now())
  users                   userOnCourse[]
  title                   String
  description             String
}

model userOnCourse {
  createdAt     DateTime    @default(now())
  user          user        @relation(fields: [userId], references: [id])
  userId        String
  course        Course      @relation(fields: [courseId], references: [id])
  courseId      String

  @@id([userId, courseId])
}

It's a many-to-many relation where one user can have many courses and one course can have many users, for referencing the explicit m-m relation I created another model names userOnCourse.

I am struggling to pass the reference of courseId into the userOnCourse model, I want to connect the course in conjunction with userid into that model but as it's created on runtime it seems not possible to pass the courseId on compile time.

Here is the code, Assume we already have user data when creating a course, How to pass the courseId that has been creating on run-time, and make a connection.

I am following this reference from prisma docs.

const createCourse = await prisma.course.create({
            data: {
                title: courseData.title,
                description: courseData.description,
                users: {
                    connect: {
                        userId_courseId: {
                            userId: user.id,
                            courseId: ?? // That is where the problem is, how can i add the coursesId?
                        },
                    },
                },
            },
        });

Upvotes: 5

Views: 3628

Answers (3)

echo
echo

Reputation: 480

None of the answers here worked for me when I was using createMany().

It wasn't until I switched to create() did the accepted answer work.

Noting in case anyone else runs into this.

Upvotes: 0

Goran Jakovljevic
Goran Jakovljevic

Reputation: 2820

In case you need to assign already existing entity:

const response: course = await prisma.course.create(
            { data: 
                { ...someOtherData, 
                    userOnCourse: { create: [ {user: { connect: { id: 2 } } } ]}
                }  
            }
        );

It's confusing since it says create but it means to create a connection with an already existing entity.

Upvotes: 0

Ryan
Ryan

Reputation: 6327

What you need is create and not connect. You need to create a new entry in the many-to-many table mapping a User with the Course. So you need to create that entry.

The code would be:

await prisma.course.create({
    data: {
      description: 'des',
      title: 'title',
      users: { create: { userId: user.id } },
    },
})

Upvotes: 9

Related Questions