Prisma through associations

If I have a model like so model CustomerAddresses { customerId Int @map("customer_id") addressId Int @unique() @map("address_id") address Address @relation(fields: [addressId], references: [id], onDelete: Cascade, onUpdate: NoAction) customer Customer @relation(fields: [customerId], references: [id], onDelete: Cascade, onUpdate: NoAction) @@map("customer_addresses") @@schema("quotes") } and if I want to find customer data and select their addresses I have to select all the customer addresses and then select the address associated to it. I'm wondering if there is a method of doing a through select where it just pulls the address and I don't need to do a select for the customerAddresses then address?
3 Replies
Prisma AI Help
Prisma AI Help2mo ago
You're in no rush, so we'll let a dev step in. Enjoy your coffee, or drop into #ask-ai if you get antsy for a second opinion!
Trikooplays
Trikooplays2mo ago
You can do this directly in the query:
const customer = await prisma.customer.findUnique({
where: { id: 1 },
include: {
customerAddresses: {
include: { address: true }, // you include here
},
},
})
const customer = await prisma.customer.findUnique({
where: { id: 1 },
include: {
customerAddresses: {
include: { address: true }, // you include here
},
},
})
orionvala
orionvalaOP5w ago
@Trikooplays Hey! Sorry if I wasn't super clear. I understand that you can pull the address through the customerAddresses. As I said though I don't want to have to map throough customerAddressess to get the address data I need. Is there way to just do something like this const customer = await prisma.customer.findUnique({ where: { id: 1 }, include: { addresses: { through: { customerAddresses: true } }, }, })

Did you find this page helpful?