Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find record with an empty relation

Say I have this prisma schema with an implicit m:n-relation of Post and Tag

model Post {
  id   String @id
  tags Tag[]
}

model Tag {
  id    Int    @id @default(autoincrement())
  posts Post[]
}

How do I find the first Post that has no associated Tags?

prisma.post.findFirst({
  where: {
    tags: {
      // are nonexistent (something like count === 0?)
    },
  },
}),

Thanks for the help 🙂

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can probably use orderBy by count of tags in ascending order and get the first one? Like that:

prisma.post.findFirst({
  orderBy: { tags: { _count: 'asc' } },
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading