Hi! I have 2 models with 1 to many relationships: ...
# orm-help
b
Hi! I have 2 models with 1 to many relationships:
Copy code
model User {
    id          String @id
    ownership   Ownership[]
}

model Ownership {
     id    String @id @default(cuid())
     user        User? @relation(fields: [userId], references: [id])
     userId      String
     createdAt   DateTime @default(now())
}
When i query a user, i want to return only 1 ownership:
Copy code
prisma.user.findUnique({
      where: {
        id: userId,
      },
      include: {
      ownership: {
      take: 1,
      orderBy: {
        createdAt: 'desc',
      },
    },
  },
})
But i want the
user.ownership
will be an object, and not array of 1
ownership
element. i.e
Copy code
result.ownership = result.ownership[0];
I want to do it for every query for User, so middleware seemed like the best approach but i can see how you can't mutate the return value of the query with middleware. Any ideas maybe?
r
You would need to change this in your application itself. A many side of the relation will always return an array even if you only fetch a single one.
b
So for every query of users i will need to manipulate the response. No way around that? maybe some different approach that you can think of?
r
Yes that’s the only way as of now.