Andreas Du Rietz
08/03/2022, 7:06 AMmodel 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?Nurul
08/03/2022, 11:17 AMmodel 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
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.
> 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 }Andreas Du Rietz
08/03/2022, 11:25 AMareaCode_startDate part especially, which I missed 🙂