drizzle-team / drizzle-team/drizzle-orm

[BUG]:Inconsistent Aliases in SQL Queries with findMany and `sql`` Template Literals

Open
#3,068 0 comments 1 reaction 0 assignees View on GitHub
bug priority rqb
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### What version of `drizzle-orm` are you using?

0.33.00

### What version of `drizzle-kit` are you using?

0.24.2

### Describe the Bug

Hi,

I am encountering an issue with table aliases when using SQL queries. I have a helper function that constructs WHERE clauses, and everything works fine unless the table name is in snake_case.

I’m using sql template literals to build the WHERE clause and sql.join to join the conditions together. Here’s an example:

sql`AND ${table[k as keyof typeof table]} LIKE '%${sql.raw(v.like)}%'`

This is how I join them and return the result in a variable:

where: q?.where ? sql.join(getWhere(table)(q.where)) : undefined

At the end, I use the findMany function to take advantage of the include option. For example:

await tx.query.inventoryTransactions.findMany({ ...q, limit, offset });

However, when executing the query, I run into an issue related to the table names. The query generated looks like this:

SELECT `id`, `note`, `type`, `status`, `supplier_invoice_number`, `order_number`, `plan_modality_activity_school_id`, `rejection_note`, `approve_note`, `created_at`, `updated_at`
FROM `inventory_transactions` `inventoryTransactions`
WHERE (`inventory_transactions`.`supplier_invoice_number` LIKE '%12%' OR `inventory_transactions`.`order_number` LIKE '%12%')
LIMIT ? -- params: [10]

When using findMany, the query automatically adds an alias (inventoryTransactions), but the `sql template does not. It use 'inventory_transactions'

On the other hand, if I use a normal SELECT, it works fine because no alias is added. For example:

await tx.select({ count: sqlcount(*) })
.from(inventoryTransactions)
.where(q.where);

This generates:

SELECT count(*)
FROM inventory_transactions
WHERE (inventory_transactions.supplier_invoice_number LIKE '%12%' OR inventory_transactions.order_number LIKE '%12%')

Is there a way to ensure consistency with aliases, or am I doing something wrong?

Thanks in advance for any help.

### Expected behavior

sql`` and findMany use the format for the aliases

### Environment & setup

node: v22.6.0
npm: 10.8.2
MacOs: 15.0.1 (24A348)

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the reported findMany query using the sql template literal and compare its generated SQL with the normal SELECT example. Trace how findMany applies the inventoryTransactions alias and how the WHERE expression references inventory_transactions; done means both forms use compatible aliases without breaking the included query.

Written by the indexing model from the issue text.

Assessment

Tech stack
sql, typescript
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.