Stripe cancel and duplicate key value violates unique constraint

Hi there,

I'm trying to add Stripe to my website and I found something strange. I guess I forgot something, but can't find the info... so:

I have that button which call
        await auth.subscription.upgrade({
            plan,
            successUrl: `/${locale}/dashboard`,
            cancelUrl: `/${locale}/pricing`,
            annual,
        })


It opens the Stripe page. On that page, if you click the "back to website" link, and try again to subscribe, you got the following error
  query: 'insert into "subscriptions" ("id", "plan", "reference_id", "stripe_customer_id", "stripe_subscription_id", "status", "period_start", "period_end", "cancel_at_period_end", "seats", "created_at", "updated_at") values ($1, $2, $3, $4, default, $5, default, default, default, $6, default, default) returning "id", "plan", "reference_id", "stripe_customer_id", "stripe_subscription_id", "status", "period_start", "period_end", "cancel_at_period_end", "seats", "created_at", "updated_at"',
  params: [Array],
  [cause]: [error: duplicate key value violates unique constraint "subscriptions_reference_id_unique"] {
    length: 275,
    severity: 'ERROR',
    code: '23505',
    detail: 'Key (reference_id)=(phDhsbH.....0bKoq9NTl) already exists.',
    hint: undefined,
    position: undefined,
    internalPosition: undefined,
    internalQuery: undefined,
    where: undefined,
    schema: 'public',
    table: 'subscriptions',
    column: undefined,
    dataType: undefined,
    constraint: 'subscriptions_reference_id_unique',
    file: 'nbtinsert.c',
    line: '673',
    routine: '_bt_check_unique'
  }
}


I'm using Drizzle and the screenshot is my better-auth config. I guess I'm missing some "cancelation" but I can't figure how/where
CleanShot_2025-06-05_at_07.24.472x.png
Was this page helpful?