It's been a while since doing SQL and first time f...
# orm-help
a
It's been a while since doing SQL and first time for prisma so I'm very rusty. I'm looking to store price entries that I imagine using a composite key like this:
Copy code
model DailyPriceEntry {
  areaCode      String
  price         Decimal
  startDate     DateTime
  @@id([areaCode, startDate])
}
However, I'm not sure how to query a unique row. The docs seem to suggest I can't use findUnique using composite keys (is that the correct term?) Will I run into more issues with composite keys and should I model it in another way?
✅ 1
n
Hey Andreas 👋 Welcome to our community! For this model
Copy code
model DailyPriceEntry {
  areaCode  String
  price     Decimal
  startDate DateTime @default(now())

  @@id([areaCode, startDate])
}
Here’s how you could query a unique row while using composite keys
Copy code
import { PrismaClient } from '@prisma/client';

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

async function main() {
  await prisma.dailyPriceEntry.create({
    data: {
      areaCode: '1',
      price: 1,
    },
  });

  const findUniqueRecord = await prisma.dailyPriceEntry.findUnique({
    where: {
      areaCode_startDate: {
        areaCode: '1',
        startDate: new Date('2022-08-03T11:12:55.490Z'),
      },
    },
  });

  console.log(findUniqueRecord);
}

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

prisma:query SELECT "public"."DailyPriceEntry"."areaCode", "public"."DailyPriceEntry"."startDate", "public"."DailyPriceEntry"."price" FROM "public"."DailyPriceEntry" WHERE ("public"."DailyPriceEntry"."areaCode" = $1 AND "public"."DailyPriceEntry"."startDate" = $2) LIMIT $3 OFFSET $4

{ areaCode: '1', price: 1, startDate: 2022-08-03T11:12:55.490Z }
a
Awesome answer! 🙌 The
areaCode_startDate
part especially, which I missed 🙂
🙏 1