Hello! I seem to have run into a bug with Prisma w...
# orm-help
n
Hello! I seem to have run into a bug with Prisma when counting relations. I have this model:
model Hashtag {
tag String @id
posts Post[]
}
And i’m trying to count the relations like so:
include: { _count: { select: {  posts: true  } } },
And it’s throwing the below error.. looks like prisma isn’t generating something correctly? Running prisma migrate dev tells me there’s nothing to generate 😞
t
Did you run
prisma generate
?
n
yep, it runs automatically when running prisma migrate dev, nothing changes
but also this looks like an issue in the database, not the generated client files
t
Oh wait, the error comes from the
Post
model, could you post that? Also if your DB has no data, I believe you can use
prismage migrate reset
to reset the databse.
n
model Post {
id        Int       @id @default(autoincrement())
createdAt DateTime  @default(now())
content   String
author    User      @relation(fields: [authorId], references: [id], onDelete: Cascade, onUpdate: Cascade)
authorId  Int
tags Hashtag[]
}
Sure, here it is, but it’s way too basic to have any mistakes imo, i think it’s just the migration tool not generating some required fields in the db
t
If you think that's the case, you can inspect the migration file, it's just a SQL. But I'm not sure why that would happen, as you said, it's a basic schema.
n
I’m trying to find what orderby_0_post.id is supposed to be, the orderby part to be precise. Do you have any idea in which table this is supposed to be? I can’t find any similar structures for other count relations that do work
Okay, I managed to fix this by using an explicit relation model like this:
model Hashtag {
tag String @id
connections HashtagConnection[]
}
// Bypass weird prisma bug
model HashtagConnection {
tagId String
postId Int
tag Hashtag @relation(fields: [tagId], references: [tag])
post Post @relation(fields: [postId], references: [id])
@@id([tagId, postId])
}
Weird as hell, but it works now, though the syntax in prisma client got much uglier
r
@Nichita Z 👋 This seems like a bug. It would be great if you could open an issue here with the above model and query so that we can look into this