[BUG]: orderBy causes relational query to fail
const shipmentQuery = await db.query.shipment.findMany({
with: {
products: true,
},
// [BUG]: orderBy causes relational query to fail
// orderBy: desc(shipment.createdAt),
});
same issue here:
https://github.com/drizzle-team/drizzle-orm/issues/1249
i cannot use orderBy
any fix?
GitHub
[BUG]: orderBy causes relational query to fail · Issue #1249 · driz...
What version of drizzle-orm are you using? 0.28.6 What version of drizzle-kit are you using? n/a Describe the Bug Hi Drizzle Team, thank you for making an awesome library! I think I found a bug, an...
10 Replies
Are you sure this is orderBy that bugs? the where is filtering on a table column that is not part of the request
if you use
where
in its functional way where: (table, { and, isNotNull }) => and(isNotNull(table.xx))
I guess you will not have product id
but only shipment
columns availableI have an exemple for filtering in nested queries but it is more a hack than a supported feature: https://drizzle.run/brfzfblyhllb7dz24d630px3
thank you for share.. So how can i use it orderBy, i just want to order shipments by createdAt?
This looks good.
Isn’t it working?
You have a createdAt in your shipment table?
yup, but it take too long until return the data, if i remove orderBy it works normally!
it takes more than 3 min with orderBy! and few second without !
you need to add index on shipment createdAt. It will be blazing faster
that what i thought, but i never do it before.. can you guide me?
is this correct?
yes.
You could also create an index for
shipmentId
in product too.
Fields and references used in relation benefit from indexes (no need for primary keys, but here shipmentId
is a foreign key for product
, so this is why I suggest to add an index too).
you can even specify .on(table.createdAt.desc())
if it is always how you order itDrizzle ORM - Indexes & Constraints
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
it works, thanks a lot @Raphaël M (@rphlmr) ⚡ 😍