fondfolio
09/28/2020, 8:59 PMfindOne
with relations fields? Our model specifically does not have a field which we can use to enforce order (via orderBy
), we just want the data read in the same order we wrote it the DB. Do we need to add an explicit order field to guarantee this order (this is what we are currently thinking of doing)? Thought we'd ask here in case anyone might have encountered this before, any help would be much appreciated, thanks! 😊Ryan
09/29/2020, 7:28 AMmodel Post {
id Int @default(autoincrement()) @id
published Boolean @default(false)
title String
content String?
authorId Int?
author User? @relation(fields: [authorId], references: [id])
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model User {
id Int @default(autoincrement()) @id
name String?
posts Post[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
I can order relation fields like this:
await prisma.user.findOne({
where: { id: 1 },
select: { posts: { orderBy: { id: 'asc' } } },
})
Is this the behaviour you were looking for?fondfolio
09/29/2020, 2:09 PMcartogram
09/30/2020, 2:10 AMUser
has Posts
) if we were writing the User and Posts as one prisma.user.create
(with a nested create
for Posts), is there a way to guarantee they are read in the same order we wrote them in? I see you are relying on the autoincrement()
id, but just wondering if there is a way around that. Thanks! 🙏Ryan
09/30/2020, 6:25 AMcreatedAt
?
await prisma.user.findOne({
where: { id: u.id },
select: {
name: true,
posts: { select: { title: true }, orderBy: { createdAt: 'asc' } },
},
})
cartogram
09/30/2020, 7:27 PMcartogram
09/30/2020, 7:27 PMorder
fieldcartogram
09/30/2020, 7:28 PMRyan
10/01/2020, 7:03 AMcreatedAt
and the other from id
. That will always be deterministic.