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
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!You can do this directly in the query:
@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
}
},
},
})