Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prisma: Finding items where two fields have the same value

Tags:

prisma

I would like to find items in a Prisma db where the values for two columns are the same. The use case is to compare the 'created_at' and 'updated_at' fields to find items that have never been updated after their initial creation. In raw SQL I would do something like:

select updated_at,
       cast(sign(sum(case when updated_at = created_at then 
          1 
       else
          0
       end)) as int) as never_modified
  from tab
 group by updated_at

Is it possible to achieve this in Prisma?

like image 641
Christopher Ehrlich Avatar asked Feb 20 '26 01:02

Christopher Ehrlich


1 Answers

you can use the preview feature fieldReference of prisma.

schema.prisma

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["fieldReference"]
}

your code

prisma.project.findMany({
  where: { created_at: prisma.project.fields.updated_at }
})
like image 94
Tom Avatar answered Feb 27 '26 08:02

Tom