0

I have a list of products, and a list of users and I want to be able to relate users to what products they have but also what quantity they have of each product.

I've setup my user schema like this:

model users {
  id                         String                       @id @default(uuid())
  email                      String                       @unique
  password                   String
  test_products              test_products[]
  users_to_test_products     users_to_test_products[]
}

I've implicitly stated the relation so that I can add in a quantity field


model users_to_test_products {
  users             users             @relation(fields: [user_id], references: [id])
  user_id           String
  test_products     test_products @relation(fields: [product_id], references: [product_id])
  product_id        String
  quantity          Int

  @@id([user_id])
}

and I've setup my product list like this:

model test_products {
  product_id                           String                       @id
  name                                 String?
  users                                users[]
  users_to_test_products               users_to_test_products []
}

I assumed I could make an update call, connecting the user to the test product and passing in the quantity but it seems that I can't and my approach is completely wrong.

await prisma.users.update({
                data: {
                    test_products: {
                        connect: UserProducts.map((product) => {
                            return {
                                product_id: product.product_id,
                                quantity: product.quantity
                            }
                        }),
                    },
                },
                where: {
                    id: userId
                },
            })

note: UserProducts is just an object array eg [{ product_id: "tTer3434", quantity: 8 }]

Can anyone point me in the right direction on the approach I need to take please?

alienbuild
  • 246
  • 2
  • 15

0 Answers0