Hi everyone! I'm curious - creating a user with ...
# orm-help
i
Hi everyone! I'm curious - creating a user with 2 links like this (docs example):
Copy code
const newUserWithLinks = await prisma
  .createUser({
    name: "Alice",
    email: "<mailto:alice@prisma.io|alice@prisma.io>",
    password: "IlikeTurtles",
    links: {
      create: [{
        description: "My first link",
        url: "<https://www.prisma.io>"
      }, {
        description: "My second link",
        url: "<https://www.howtographql.com>"
      }]
    }, 
  })
After checking the created documents, with the mongo shell, I can see the user document has a
links
property which is an array containing the two newly created link IDs. All good, but when I check the
links
documents I don't see any
postedBy
property nor any other property that points back to the user. My question is - how does Prisma resolve the
postedBy
field?
w
What does your schema look like, probably has something to do with naming your relations?
Does postedBy actually get resolved by prisma? 🤔
i
this is how the datamodel looks like:
Copy code
type Link {
  id: ID! @id
  description: String!
  url: String!
  postedBy: User!
  votes: [Vote!]! @relation(link: INLINE)
}

type User {
  id: ID! @id
  name: String!
  email: String! @unique
  password: String!
  links: [Link!]! @relation(link: INLINE)
  votes: [Vote!]! @relation(link: INLINE)
}

type Vote {
  id: ID! @id
  link: Link!
  user: User!
}
If I move the
@relation
directive to the other end, it will save the reference there - so links will have a
postedBy
property pointing to the user but the user won't have the
links
property anymore
It does resolve though, in both cases. If I do
prisma.links().postedBy()
I get back the user.
d
We only store the id on one side i.e.
User
and will do a lookup query if you’re trying to traverse the relation coming from the other side
Vote
. Something like find all
Users
who have the id of
Vote
in their
votes
field. The
votes
field is then also indexed for better performance. This is why the side you have the
link:INLINE
on can be important for performance.
i
@do4gr, yes, I thought so too, it’s the only way you can. I’ve also checked the mongo logs which confirmed it. But is there a reason why this approach was decided? As far as I can tell, this is not the best way to do it. Are there any constraints?