hey all. I’m new to back end and I’m trying to bui...
# orm-help
f
hey all. I’m new to back end and I’m trying to build a follow unfollow with prisma and I can’t make it work. my schema
Copy code
model User {
  id         Int     @id @default(autoincrement())
  email      String  @unique
  password   String
  username   String
  bio        String?
  image      String?
  posts      Post[]
  followedBy User[]  @relation("UserFollows")
  following  User[]  @relation("UserFollows")
}
I'm trying to update like this but is not working. I'm pretty sure I messed up somewhere
Copy code
await prisma.user.update({
      where: {
        id: userLogged,
      },
      data: {
        following: {
          id: //user to be followed
        },
      },
    });
e
I think this needs a
connect
object in the latest versions of Prisma: https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries#connect-an-existing-record
Copy code
await prisma.user.update({
      where: {
        id: userLogged,
      },
      data: {
        following: {
          connect: { id }
        },
      },
    });
Are you using TypeScript 🙂 ? It’ll give you type support which would make it easier to figure out the shape of the argument
f
No, just plain js 😂
I tried using the
connect
  object but it doesn't find following as a argument in my schema
e
That’s odd, I’ve set up a codesandbox here: https://codesandbox.io/s/recursing-sutherland-zirfp ( Using nestjs template b/c I couldn’t find a Node.js TS template - but anyways ) Here’s the schema that is similar to yours: https://codesandbox.io/s/recursing-sutherland-zirfp?file=/prisma/schema.prisma Here’s a file that calls prisma client and generated type looks correct ( No TS error ): https://codesandbox.io/s/recursing-sutherland-zirfp?file=/src/main.ts
Not sure what would be the problem in your case 🤔 . Here are some things you could try: • Check Prisma version and try bumping to the latest ( v2.18 ) • Run
yarn prisma generate
and try running the query again • Check that the user records exist Otherwise, I’m out of ideas 😅 Hopefully someone else in the channel can help!
r
@Filippo Barcellos 👋 Yes as Eddy mentioned, just a connect is required. Could you share the exact query you’re using and also if the
id
you’re passing to connect exists in the database?
f
Thanks Eddy and Ryan. I was messing up with my migrations so it was not a prisma problem. Follow this same schema structure how could I create a query that returns only users that are not follow by me? Thanks again
r
@Filippo Barcellos E.g if you want user’s that do not follow the user with
id
1, you can do the following:
Copy code
await prisma.user.findMany({
      where: { AND: [{ following: { none: { id: 1 } } }, { id: { not: 1 } }] },
})
f
@Ryan awesome. Thanks for your help!
💯 1