Relational query problem

My goal is to get object of this shape:
product: {
...
images: [{...}, {...}, {...}]
}
product: {
...
images: [{...}, {...}, {...}]
}
Product has relation to images - one-to-many,
export const productRelations = relations(products, ({ many}) => ({
images: many(images)
}))
export const productRelations = relations(products, ({ many}) => ({
images: many(images)
}))
and according to the docs: https://orm.drizzle.team/docs/joins#many-to-one-example I should be able to do this:
const products = await db.select().from(products)
.leftJoin(images, eq(images.productId, products.id)).all()
const products = await db.select().from(products)
.leftJoin(images, eq(images.productId, products.id)).all()
(using postgres) However, apparently, method all() does not exist and I get an error: db.select(...).from(...).leftJoin(...).all is not a function. If I'm doing something wrong please tell me.
2 Replies
Noahh
Noahh14mo ago
the all is for SQLite (see the import in that code snippet) and is not needed for postgres. Also note that the relation you defined will not work with db.select()... you have to use the RQB like db.query.products.findMany({ with: { images: true} }) See https://orm.drizzle.team/docs/rqb#foreign-keys for more information.
ArwanceR
ArwanceR14mo ago
Ah ok, thank you for your answer. I know I could do this with findMany()` but I wanted to try do it the other way, so I could include aggregating function which is not supported "as of now" with relational queries.
Want results from more Discord servers?
Add your server