Anyone had a unique where clause not working with ...
# orm-help
d
Anyone had a unique where clause not working with a string, enum, and Date? For some reason a findFirst works but not a findUnique or a nested connect - the findUnique returns null and the connect fails while the firsdFirst finds the only unique record .
Copy code
const findUnique = await this.prisma.availability.findUnique({
  where: {
    propertyId_date_status: {
      propertyId: 'cki4d4rn102310qqlri351ppf',
      status: AvailabilityStatus.AVAILABLE,
      date: new Date('2020-12-02T00:00:00.000Z'),
    }
  }
});

const findFirst = await this.prisma.availability.findFirst({
  where: {
    propertyId: 'cki4d4rn102310qqlri351ppf',
    status: AvailabilityStatus.AVAILABLE,
    date: new Date('2020-12-02T00:00:00.000Z'),
  }
});

console.log(findUnique); // <-- Null
console.log(findFirst); // <-- Not null

model Availability {
  date          DateTime
  property      Property              @relation(fields: [propertyId], references: [id])
  propertyId    String
  status        AvailabilityStatus    @default(AVAILABLE)

  @@unique([propertyId, date, status])
}

enum AvailabilityStatus {
  AVAILABLE
}
r
Hey @Dustin 👋 Could you share your Prisma version? I would try if I can reproduce this.
d
Sure @Ryan! Its version 2.12.1 - i had the same issue with 2.11.0 but I had to update as the Date wasnt working which got fixed with 2.12
r
I cant seem to reproduce the above with 2.12.1. Creating a schema from the above snippet:
Copy code
model Property {
  id           String         @id @default(cuid())
  Availability Availability[]
}

model Availability {
  date       DateTime
  property   Property           @relation(fields: [propertyId], references: [id])
  propertyId String
  status     AvailabilityStatus @default(AVAILABLE)

  @@unique([propertyId, date, status])
}

enum AvailabilityStatus {
  AVAILABLE
}
I created an availability and passed the same parameters that I received from the response and I can get the response fine in
findFirst
and
findUnique
.
👍 1
d
Hmmm okay im trying to figure this out - weirdly enough my findUnique fails if its on its own, but if I run findFirst before it, then both succeed. I'm going to try nail down exactly whats wrong and put together a repo for you to look at
💯 2
Thanks for looking at this mate!
🙌 2
I found it! Its been a long night. The problem is that, in my setup i had 2 versions of the same service running. This meant that my findUnique was running twice in parallel. So to recreate the issue, you can try this where 2 of the same unique queries running together returns null. Might be worth having an error for this or merging into a single sql query:
Copy code
const firstUnique = prisma.availability.findUnique({
    where: {
      propertyId_date_status: {
        propertyId: 'cki5rxlf700018txwmruu3qjx',
        status: 'AVAILABLE',
        date: new Date(date),
      }
    },
  });

  const secondUnique = prisma.availability.findUnique({
    where: {
      propertyId_date_status: {
        propertyId: 'cki5rxlf700018txwmruu3qjx',
        status: 'AVAILABLE',
        date: new Date(date),
      }
    },
  });

  const res = await Promise.all([firstUnique, secondUnique])
💯 1
r
This calls for an issue. It would be great if you could create one here 🙂
d
Sure will do 🙂
🙌 1