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

[FEATURE]: Support multiple onConflictDoUpdate's #2276

Open
mw10013 opened this issue May 8, 2024 · 0 comments
Open

[FEATURE]: Support multiple onConflictDoUpdate's #2276

mw10013 opened this issue May 8, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@mw10013
Copy link

mw10013 commented May 8, 2024

Describe what you want

When an insert has multiple onConflictDoUpdate's, it seems that only the last one is picked up when executed. Ask for multiple on conflict clauses per https://www.sqlite.org/syntax/upsert-clause.html

    const [subscription] = await db
      .insert(schema.subscriptions)
      .values({
        id: subscriptionData.id,
        status: subscriptionData.status,
        plan,
        cycleStartedAt,
        customerId: subscriptionData.customerId,
        createdAt,
        updatedAt,
      })
      .onConflictDoUpdate({
        target: schema.subscriptions.id,
        set: {
          status: subscriptionData.status,
          plan,
          cycleStartedAt,
          updatedAt,
        },
        setWhere: sql`${updatedAt.getTime()} > ${schema.subscriptions.updatedAt}`,
      })
      .onConflictDoUpdate({
        target: schema.subscriptions.customerId,
        set: {
          id: subscriptionData.id,
          status: subscriptionData.status,
          plan,
          cycleStartedAt,
          createdAt,
          updatedAt,
        },
        setWhere: sql`${createdAt.getTime()} > ${schema.subscriptions.createdAt}`,
      })
      .returning();
insert into "subscriptions" ("id", "status", "plan", "cycle_started_at", "customer_id", "created_at", "updated_at") 
values (?, ?, ?, ?, ?, ?, ?) 
on conflict ("subscriptions"."customer_id") 
do update set "id" = ?, "status" = ?, "plan" = ?, "cycle_started_at" = ?, "created_at" = ?, "updated_at" = ? 
where ? > "subscriptions"."created_at" returning "id", "status", "plan", "cycle_started_at", "customer_id", "created_at", "updated_at" 
@mw10013 mw10013 added the enhancement New feature or request label May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant