drizzle-team / drizzle-team/drizzle-orm
[BUG]: `sql` in D1 batch causes "TypeError: Cannot read properties of undefined (reading 'bind')"
- 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.30.10
### What version of `drizzle-kit` are you using?
0.20.18
### Describe the Bug
Using `sql` in D1 batch causes "TypeError: Cannot read properties of undefined (reading 'bind')"
```ts
const upsertSql = sql`insert into ${schema.subscriptions}
(id, status, plan, cycle_started_at, customer_id, created_at, updated_at)
values (${subscriptionData.id}, ${subscriptionData.status}, ${plan}, ${cycleStartedAt.getTime()}, ${subscriptionData.customerId}, ${createdAt.getTime()}, ${updatedAt.getTime()})
on conflict (${schema.subscriptions.id}) do update set status = ${subscriptionData.status}, plan = ${plan}, cycle_started_at = ${cycleStartedAt.getTime()}, updated_at = ${updatedAt.getTime()}
where ${updatedAt.getTime()} > ${schema.subscriptions.updatedAt}
on conflict (${schema.subscriptions.customerId}) do update set id = ${subscriptionData.id}, status = ${subscriptionData.status}, plan = ${plan}, cycle_started_at = ${cycleStartedAt.getTime()}, created_at = ${createdAt.getTime()}, updated_at = ${updatedAt.getTime()}
where ${createdAt.getTime()} > ${schema.subscriptions.createdAt}
returning *`;
const [[batchSubscription]] = await db.batch([
db.all(upsertSql),
]);
```
Running the sql outside of batch works fine.
```ts
const [subscription] = await db.all(upsertSql);
```
```sql
insert into "subscriptions"
(id, status, plan, cycle_started_at, customer_id, created_at, updated_at)
values (?, ?, ?, ?, ?, ?, ?)
on conflict ("subscriptions"."id") do update set status = ?, plan = ?, cycle_started_at = ?, updated_at = ?
where ? > "subscriptions"."updated_at"
on conflict ("subscriptions"."customer_id") do update set id = ?, status = ?, plan = ?, cycle_started_at = ?, created_at = ?, updated_at = ?
where ? > "subscriptions"."created_at"
returning *
-- params: ["sub_01hx0g3byqxvh7qk2hqvpgmx3a", "active", "pro", 1715184686922, "ctm_01hrfh9t7c2d97p62gdmcvvvgg", 1714782580695, 1715184688678, "active", "pro", 1715184686922, 1715184688678, 1715184688678, "sub_01hx0g3byqxvh7qk2hqvpgmx3a", "active", "pro", 1715184686922, 1714782580695, 1715184688678, 1714782580695]
```
### Expected behavior
`sql` works in D1 batch as it does outside of D1 batch.
### Environment & setup
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.