Hi everyone. I'm having problem updating and delet...
# orm-help
n
Hi everyone. I'm having problem updating and deleting a row of a junction table in a many to many relationship model. The update and delete method works for regular tables without relationship of any kind but doesn't work for relationship tables. Example of my code is something like this:
Copy code
const update = await prisma.categories.update({ 
    where: { 
        id: req.params.id
    }, 
    data:{ 
        status: "disabled" 
    }
});
I would have sent the error here but my PC is off RN. I'm new to Prisma and I'm not sure whether I'm doing anything wrong here, please help!
r
@Nothing. 👋 Could you share this part of your schema? And is
status
what you’re updating a relation field?
You can also check this section which has and example and might cover your use case.
n
Thank you @Ryan I tried those too and I get this error:
Copy code
PrismaClientValidationError: 
Invalid prisma.update() invocation in
/home/abdulrahman/Documents/Projects/intercord/backend/src/database/repositories/memberships.repository.ts:67:25

   64 async deleteOneByUser(data: any) {
   65   let company;
   66   try {
→  67           company = await this.update{
        where: {
          company_id: 1
          ~~~~~~~~~~
        },
        data: {
          is_deleted: true
        }
      })

Unknown arg company_id in where.company_id for type membershipsWhereUniqueInput. Available args:

type membershipsWhereUniqueInput {
  id?: Int
}
Im trying it with a similar use case above
r
You need to change
company_id
to
id
as per the error
n
Yeah @Ryan thank you, it worked but I'll have to run multiple queries to achieve a state where the right company will be actually updated. For instance, if I want to update a membership of a user with id of 3 and a company with id of 5, then I'd have to first run a findFirst() query first?
Copy code
let membership = await prisma.memberships.findFirst({
  where: { 
    user_id: 4, 
    conpany_id: 5 
  }
});
Then proceed to do the actual update with the above result:
Copy code
await prisma.memberships.update({ 
  where: { 
    id: membership.id 
  }, 
  data: { /**/ }
});
But I want to just do that in one place like this:
Copy code
await prisma.memberships.update({ 
   where: { 
     user_id: ...
     company_id: ...
   }, 
   data: { /**/ }
 });
Well, I think the reason I'm getting the error I that the
user_id
and
company_id
aren't unique columns
r
You need to use
updateMany
in that case.
Copy code
let membership = await prisma.memberships.updateMany({
  where: { 
    user_id: 4, 
    conpany_id: 5 
  },
  data: {
    // data to update
  }
});
👍 1
n
Thank you, I'll try that @Ryan
👍 1
m
Just to add to @Ryan ‘s solution, you could do this also: You need to use
updateMany
with the AND operator in that case. AND is not required if you pass the arguments to the WHERE clause as @Ryan did
Copy code
let membership = await prisma.memberships.updateMany({
  where: 
    AND: [
    {user_id: 4},
    {conpany_id: 5}
  ],
  data: {
    // data to update
  }
});
prisma green 1
💯 1
n
Thank you @Martïn, I think this is better than just using the where clause.
m
Hi @Nothing. Are you from Nigeria? 🇳🇬
I can help you with Prisma. If you have friends also looking to pick up steam with Prisma, let them know about Prisma. We’re always here to help
n
Yeah I'm from Nigeria @Martïn. I'll learn about them, Thank you for your apt response.
m
I am also from Nigeria 🇳🇬
🇳🇬 1
n
I have told most of my friends about prisma, they liked the syntax.
Wow, that's awesome. Where do you stay?
m
It’s so cool. I enjoy using it everyday. My
schema.prisma
file is like 4279 LOC already.
n
Wow, I come from objection.js background, so I was not familiar with files like the prisma "schema.prisma" files. I think the current lines of my current project is ~200 LOC
Can we continue this discussion via DM? I have some questions which may not be related to prisma but definitely a coding related questions. @Martïn