Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: sql in D1 batch causes "TypeError: Cannot read properties of undefined (reading 'bind')" #2277

Open
mw10013 opened this issue May 8, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@mw10013
Copy link

mw10013 commented May 8, 2024

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')"

    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<schema.Subscription>(upsertSql),
    ]);

Running the sql outside of batch works fine.

    const [subscription] = await db.all<schema.Subscription>(upsertSql);
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

@mw10013 mw10013 added the bug Something isn't working label May 8, 2024
@mw10013
Copy link
Author

mw10013 commented May 8, 2024

Note that converting the sql to query and putting that into D1 batch directly and not via drizzle works.

const sqliteDialect = new SQLiteSyncDialect();
const upsertQuery = sqliteDialect.sqlToQuery(upsertSql);
const [result] = await D1.batch([
    D1.prepare(upsertQuery.sql).bind(...upsertQuery.params),
]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant