© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago•
1 reply
pastilhas

magic sql column naming

Ok, here's a doozy:

One of the things i like best about drizzle vs prisma is the fact that we can keep the standard naming conventions at the database level and still get the objects with cool camelCase properties. For example:
export const Purchases = pgTable('purchases', {
  id: bigserial('id', { mode: 'number' }).primaryKey(),
  websiteId: bigint('website_id', { mode: 'number' }),
  experimentId: bigint('experiment_id', { mode: 'number' }),
  variantId: bigint('variant_id', { mode: 'number' }),
  from: bigint('from', { mode: 'number' }),
  currencyCode: varchar('currency_code', { length: 4 }),
  purchases: integer('purchases'),
  revenue: bigint('revenue', { mode: 'number' }),
});
export const Purchases = pgTable('purchases', {
  id: bigserial('id', { mode: 'number' }).primaryKey(),
  websiteId: bigint('website_id', { mode: 'number' }),
  experimentId: bigint('experiment_id', { mode: 'number' }),
  variantId: bigint('variant_id', { mode: 'number' }),
  from: bigint('from', { mode: 'number' }),
  currencyCode: varchar('currency_code', { length: 4 }),
  purchases: integer('purchases'),
  revenue: bigint('revenue', { mode: 'number' }),
});


Yesterday, i was working on a custom query, so i decided to use the magical sql operator, provided by drizzle ORM.

sql`
  SELECT 
    ${Purchases.variantId}, 
    ${Purchases.currencyCode}, 
    ${Purchases.from}, 
    SUM(${Purchases.revenue}) AS revenue
  FROM 
    ${Purchases}
  WHERE 
    ${Purchases.variantId} IN (
      Select ${Variants.id} From ${Variants} WHERE ${Variants.experimentId} = ${experimentId}
    ) 
    AND ${Purchases.currencyCode} = ${currency}
  GROUP BY 
    ${Purchases.variantId}, 
    ${Purchases.currencyCode},
    ${Purchases.from}
  ORDER BY 
    ${Purchases.from} ASC;
`;
sql`
  SELECT 
    ${Purchases.variantId}, 
    ${Purchases.currencyCode}, 
    ${Purchases.from}, 
    SUM(${Purchases.revenue}) AS revenue
  FROM 
    ${Purchases}
  WHERE 
    ${Purchases.variantId} IN (
      Select ${Variants.id} From ${Variants} WHERE ${Variants.experimentId} = ${experimentId}
    ) 
    AND ${Purchases.currencyCode} = ${currency}
  GROUP BY 
    ${Purchases.variantId}, 
    ${Purchases.currencyCode},
    ${Purchases.from}
  ORDER BY 
    ${Purchases.from} ASC;
`;


The problem here is that the resulting objects i get come with the database column naming:
variant_id
variant_id
,
currency_code
currency_code
...

Even if i try using
AS
AS
, the return doesn't keep the capitalize characters in the column name

 SELECT 
    ${Purchases.variantId} as variantId, 
    ${Purchases.currencyCode} as currencyCode, 
 SELECT 
    ${Purchases.variantId} as variantId, 
    ${Purchases.currencyCode} as currencyCode, 

still gives me back:

{
  variantid,
  currencycode
}
{
  variantid,
  currencycode
}


My questions are:
- Is there a way to make this work as expected?
- if not, why should i use the drizzle magic sql operator, instead of just a normal call through the postgres driver, which would allow me to just write pure SQL instead of having to use:
${Purchases.variantId}
${Purchases.variantId}
?

TY
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Magic sql'' operator concat
Drizzle TeamDTDrizzle Team / help
11mo ago
Magic sql`` operator array
Drizzle TeamDTDrizzle Team / help
3y ago
Column name different in drizzle magic sql vs drizzle-orm
Drizzle TeamDTDrizzle Team / help
2y ago
sql magic operator with dates?
Drizzle TeamDTDrizzle Team / help
15mo ago