hey, so I’m trying to query a model called Timerep...
# orm-help
k
hey, so I’m trying to query a model called Timereport and in it I have a relation to another User model. I basically want to search for all entries created by user X between some dates (I have a date field in the timereport model). Can’t figure it out. Just getting null on the user field when I run include and or select. maybe somebody has some input that could point me in the right direction.
✅ 1
prisma schema
Copy code
// Timereport

model Timereport {
  id          String   @id @default(cuid())
  date        String
  event       String
  hours       Float
  user        User?    @relation(fields: [email], references: [email])
  email       String?  @unique
  locked      Boolean  @default(false)
  project     Project? @relation(fields: [projectName], references: [name])
  projectName String?

  @@unique([date, event]) // Do not allow multiple entries of same date + event
}

model User {
  id            String       @id @default(cuid())
  name          String?
  email         String?      @unique
  emailVerified DateTime?
  image         String?
  accounts      Account[]
  sessions      Session[]
  role          Role         @default(USER)
  timereport    Timereport[]
}
basically trying to query with findMany on the timereport table but include everything from one specific user matching user.email
n
Hey 👋 Based on your model here’s a query which would provide time reports for a user between a date range.
Copy code
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient({
  log: ['query'],
});

async function main() {
  await prisma.user.create({
    data: {
      email: 'sundarani@prisma.io',
      emailVerified: new Date(),
      name: 'Sundarani',
      timereport: {
        create: {
          date: '2020-01-01',
          event: 'test',
          hours: 1,
        },
      },
    },
  });

  const reports = await prisma.timereport.findMany({
    where: {
      email: 'sundarani@prisma.io',
      date: {
        lt: '2022-01-01',
        gt: '2019-01-01',
      },
    },
    include: {
      user: true,
    },
  });

  console.log('reports', reports);
}

main()
  .catch((e) => {
    throw e;
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
Here’s the output:
Copy code
> ts-node index.ts

prisma:query SELECT "public"."Timereport"."id", "public"."Timereport"."date", "public"."Timereport"."event", "public"."Timereport"."hours", "public"."Timereport"."email", "public"."Timereport"."locked", "public"."Timereport"."projectName" FROM "public"."Timereport" WHERE ("public"."Timereport"."email" = $1 AND "public"."Timereport"."date" < $2 AND "public"."Timereport"."date" > $3) OFFSET $4
prisma:query SELECT "public"."User"."id", "public"."User"."name", "public"."User"."email", "public"."User"."emailVerified", "public"."User"."image" FROM "public"."User" WHERE "public"."User"."email" IN ($1) OFFSET $2
reports [
  {
    id: 'cl6dlbaoo0001cwlffzrkmwod',
    date: '2020-01-01',
    event: 'test',
    hours: 1,
    email: 'sundarani@prisma.io',
    locked: false,
    projectName: null,
    user: {
      id: 'cl6dlbaon0000cwlfgrv2zzja',
      name: 'Sundarani',
      email: 'sundarani@prisma.io',
      emailVerified: 2022-08-03T12:33:19.219Z,
      image: null
    }
  }
]
❤️ 1
k
wow interesting! Thnx for the query. Quick question - I see that you actually create a user and a timereport at the same time
I however use Next-auth which automatically creates the user for me on login (google provider). So when the user is logged in and submits a form they actually just have to create the Timereport table population
I seem to have issue “connecting” the relation between the user. and timereport
so I can query which users created which timereports
I thought the issue was with my prisma schema but looking at your example it seems to be related to how the query was written
I really appreciate the time you’ve put into providing me with an example
n
No worries at all, Just for sake of not writing two separate queries to create a user and timereport I combined them, but you can create them separately as well. It shouldn’t change the behaviour. 👍
k
got it thnx so much