Valerio
Valerio
DTDrizzle Team
Created by Valerio on 10/18/2023 in #help
orderBy with variable column
Hey all! I'd like to explore the possibility to order a query by one of it's columns, but the column itself is inferred from a variable. What i've tried:
// "categories" has 2 column, id and name

const sortField = 'name';

await db.query.categories.findMany({ orderBy: asc(sql`${sortField}`) });
// "categories" has 2 column, id and name

const sortField = 'name';

await db.query.categories.findMany({ orderBy: asc(sql`${sortField}`) });
but no sort is applied. Even using
const sortField = 'name' as keyof typeof categories;
const sortField = 'name' as keyof typeof categories;
does not change the final result. Any idea? Thanks in advance!
9 replies