0

I am trying to figure out how I can model a many to many relationship in Prisma, using typescript.

I have models called User and Issue. Users can have many Issues and an issue can belong to many Users.

I am okay making the issue owned by one User, but I am trying to understand how to add an array (and what the identifers can be) when I make the issue have many users.

In my prisma schema I have:

model Issue {
  id              String      @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  title           String
  description     String
  members          User     @relation(fields: [memberId], references: [id])

// If i try members User[] and repeat the above relation field, I get red underline in VS Code which suggests a problem memberId String @db.Uuid createdAt DateTime @default(now()) @db.Timestamptz(6) updatedAt DateTime @default(now()) @updatedAt @db.Timestamptz(6) }

model User {
  id        String   @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  email     String   @unique
  password  String
  firstName String
  lastName  String
  role      Role     @default(USER)
  createdAt DateTime @default(now()) @db.Timestamptz(6)
  updatedAt DateTime @default(now()) @updatedAt @db.Timestamptz(6)
  issues    Issue[]
}

How can I make the members record in the Issue model an array? How can I specify that each member id should be the user id?

1 Answer 1

0

From the schema you shared, you are trying to do an implicit many-to-many relation between Users and Issues.

enum Role {
  USER
  ADMIN
}

model Issue {
  id              String      @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  title           String
  description     String
  members          User[]    
}

model User {
  id        String   @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  email     String   @unique
  password  String
  firstName String
  lastName  String
  role      Role     @default(USER)
  createdAt DateTime @default(now()) @db.Timestamptz(6)
  updatedAt DateTime @default(now()) @updatedAt @db.Timestamptz(6)
  issues    Issue[]
}

From the schema, we can see that an Issue can have many Users and Users can also have many Issues.

To create an issue with multiple users, we can write the code below

await prisma.issue.create({
    data: {
      title: 'How to become a butterfly',
      description: 'I want to become a butterfly',
      members: {
        create: [
           { email: '[email protected]', password: 'test1', firstName:'Ralph', lastName:'Wiggum' },
           { email: '[email protected]', password: 'test2', firstName:'Will', lastName:'Baker' },
        ],
      },
    },
  })

To view the issue and users associted with the issue, you can query it as shown

 const res = await prisma.issue.findMany({
    include: {
      members: true,
    },
  });

The output is

[
  {
    id: 'f24e7383-21de-4a65-bc59-4583e62a1278',
    title: 'How to become a butterfly',
    description: 'I want to become a butterfly',
    members: [
      {
        id: '8534973c-f133-42ff-9443-a5d27b9ac690',
        email: '[email protected]',
        password: 'test1',
        firstName: 'Ralph',
        lastName: 'Wiggum',
        role: 'USER',
        createdAt: 2022-11-28T09:00:29.048Z,
        updatedAt: 2022-11-28T09:00:29.048Z
      },
      {
        id: 'c3bdbbcc-0a87-4b61-9af4-69d93f503af9',
        email: '[email protected]',
        password: 'test2',
        firstName: 'Will',
        lastName: 'Baker',
        role: 'USER',
        createdAt: 2022-11-28T09:00:29.048Z,
        updatedAt: 2022-11-28T09:00:29.048Z
      }
    ]
  }
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.