Skip to content

feat: support drizzle relations v2#6913

Open
ping-maxwell wants to merge 92 commits intobetter-auth:mainfrom
ping-maxwell:feat/drizzle-relations-2
Open

feat: support drizzle relations v2#6913
ping-maxwell wants to merge 92 commits intobetter-auth:mainfrom
ping-maxwell:feat/drizzle-relations-2

Conversation

@ping-maxwell
Copy link
Copy Markdown
Member

@ping-maxwell ping-maxwell commented Dec 20, 2025

closes #6766, #5931

Notes

This PR introduces a new package: drizzle-adapter as @better-auth/drizzle-adapter which will need to be published on npm.
This PR also introduces better-auth/adapters/test-adapter as an export to the better-auth adapter test utility and all of it's tests. The reason I didn't export those functions right under better-auth/adapter was to avoid issues related to those functions needing the vitest import which could be bundled into other code if exported purely under /adapter.


Summary by cubic

Adds first-class support for Drizzle Relations v2 via a new @better-auth/drizzle-adapter/relations-v2 adapter and updates the CLI to generate v2 schema and relations. Enables join queries using Drizzle’s v2 relations without changing your database.

  • New Features

    • New relations-v2 adapter for pg, mysql, and sqlite with optional transactions, camelCase, pluralization, and debug logs.
    • Adapter-powered schema generation (createSchema) that outputs both schema and defineRelations; the CLI now prefers the adapter’s generator when available and falls back to built-ins.
  • Migration

    • Install: npm install @better-auth/drizzle-adapter
    • Update: import { drizzleAdapter } from "@better-auth/drizzle-adapter/relations-v2" and set provider ("sqlite" | "pg" | "mysql").
    • Generate: npx @better-auth/cli generate
    • Drizzle init: pass both schema and relations from the generated file to drizzle()

Written for commit 627eccb. Summary will update on new commits.

@vercel
Copy link
Copy Markdown

vercel bot commented Dec 20, 2025

@ping-maxwell is attempting to deploy a commit to the better-auth Team on Vercel.

A member of the Team first needs to authorize it.

@ping-maxwell ping-maxwell marked this pull request as draft December 20, 2025 19:01
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Dec 20, 2025

Open in StackBlitz

better-auth

npm i https://pkg.pr.new/better-auth/better-auth@6913

@better-auth/cli

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/cli@6913

@better-auth/core

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/core@6913

@better-auth/drizzle-adapter

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/drizzle-adapter@6913

@better-auth/electron

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/electron@6913

@better-auth/expo

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/expo@6913

@better-auth/i18n

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/i18n@6913

@better-auth/kysely-adapter

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/kysely-adapter@6913

@better-auth/memory-adapter

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/memory-adapter@6913

@better-auth/mongo-adapter

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/mongo-adapter@6913

@better-auth/oauth-provider

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/oauth-provider@6913

@better-auth/passkey

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/passkey@6913

@better-auth/prisma-adapter

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/prisma-adapter@6913

@better-auth/redis-storage

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/redis-storage@6913

@better-auth/scim

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/scim@6913

@better-auth/sso

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/sso@6913

@better-auth/stripe

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/stripe@6913

@better-auth/telemetry

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/telemetry@6913

@better-auth/test-utils

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/test-utils@6913

commit: 627eccb

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5 issues found across 15 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="docs/content/docs/adapters/drizzle.mdx">

<violation number="1" location="docs/content/docs/adapters/drizzle.mdx:180">
P2: Inconsistent CLI command format. The rest of the documentation uses `@better-auth/cli@latest` to ensure users get the latest version. This is particularly important here since users need the latest CLI to generate the v2 relation schema format.</violation>
</file>

<file name="packages/drizzle-adapter/drizzle-adapter.ts">

<violation number="1" location="packages/drizzle-adapter/drizzle-adapter.ts:434">
P1: Missing null check for `db.query` before accessing `db.query[model]`. Unlike `findOne` which correctly checks `!db.query || !db.query[model]`, this will throw a TypeError if `db.query` is undefined.</violation>
</file>

<file name="packages/drizzle-adapter/test/adapter.drizzle.sqlite.test.ts">

<violation number="1" location="packages/drizzle-adapter/test/adapter.drizzle.sqlite.test.ts:73">
P2: The SQLite database connection is not closed in `onFinish`. Unlike the PostgreSQL and MySQL tests which properly close their connections (`cleanupDatabase(true)` and `mysqlDB.end()` respectively), this test leaves the connection open. Add `sqliteDB.close()` for consistency and proper resource cleanup.</violation>
</file>

<file name="packages/drizzle-adapter/test/generate-schema.ts">

<violation number="1" location="packages/drizzle-adapter/test/generate-schema.ts:31">
P2: Rule violated: **Enforce Consistent Naming Conventions**

Single-letter function name `i` and parameter `x` are not descriptive. The function performs dynamic imports with cache clearing, so consider renaming to something like `dynamicImport` or `importFresh`, and rename `x` to `modulePath`.</violation>
</file>

<file name="packages/drizzle-adapter/test/adapter.drizzle.mysql.test.ts">

<violation number="1" location="packages/drizzle-adapter/test/adapter.drizzle.mysql.test.ts:58">
P2: Rule violated: **Enforce Consistent Naming Conventions**

Variable `tables_result` uses snake_case instead of camelCase. For consistency with the rest of the file and standard JavaScript conventions, rename to `tablesResult`.</violation>
</file>

Reply to cubic to teach it or ask questions. Re-run a review with @cubic-dev-ai review this PR

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 17 files (changes from recent commits).

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="packages/drizzle-adapter-new/package.json">

<violation number="1" location="packages/drizzle-adapter-new/package.json:11">
P3: Repository metadata points to the wrong folder, so npm will link to a non-existent path. Update the repository.directory to the actual package directory.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@half2me
Copy link
Copy Markdown

half2me commented Jan 26, 2026

Hey just a heads-up. After updaing to dc16536 from dd7b8c9 the experimental joins feature stopped working for me. I've had to disable it to get things working again.

Unknown relational filter field: "decoder"
# SERVER_ERROR:  DrizzleError: Unknown relational filter field: "decoder"

@dotnize
Copy link
Copy Markdown

dotnize commented Jan 26, 2026

Hey just a heads-up. After updaing to dc16536 from dd7b8c9 the experimental joins feature stopped working for me. I've had to disable it to get things working again.

Unknown relational filter field: "decoder"
# SERVER_ERROR:  DrizzleError: Unknown relational filter field: "decoder"

@half2me I also just ran into this a few days ago. In my case I think this was when @better-auth/drizzle-adapter was actually released to npm, but without relations v2 support. For some reason my app installed both versions, the one on npm and the PR version, but the import only used the one on npm despite my package.json specifying the PR version.

The fix for me was to add an override depending on your package manager, e.g. for npm package.json:

{
  // ...
  "overrides": {
    "@better-auth/drizzle-adapter": "https://pkg.pr.new/better-auth/better-auth/@better-auth/drizzle-adapter@6913"
  }
}

And update the import to append /relations-v2

import { drizzleAdapter } from "@better-auth/drizzle-adapter/relations-v2";

Now it should work with the experimental joins feature again.

@half2me
Copy link
Copy Markdown

half2me commented Jan 26, 2026

I didn't have to touch overrides, this part was the key for me:

import { drizzleAdapter } from "@better-auth/drizzle-adapter/relations-v2";

I used to just have drizzle-adapter with /relations-v2

Thanks!

@margaretjoanmiller
Copy link
Copy Markdown

Any timeline on when this will be merged? Been a minute and this fixes a show-stopping bug on drizzle's beta

@ping-maxwell
Copy link
Copy Markdown
Member Author

Any timeline on when this will be merged? Been a minute and this fixes a show-stopping bug on drizzle's beta

it will be merged alongside Drizzle's major release.

@alexanderniebuhr
Copy link
Copy Markdown

alexanderniebuhr commented Feb 11, 2026

@ping-maxwell can we get #7169 also included in the preview package for this pr? Right now I either have to choose, custom schema vs relations v2 😓

@adamkasper
Copy link
Copy Markdown

adamkasper commented Feb 18, 2026

@ping-maxwell do we need to update relations in packages/cli/src/generators/drizzle.ts to use the new Relations v2 defineRelations/defineRelationsPart API?

const tableRelation = `export const ${relationExportName} = relations(${getModelName(

const tableRelation = `export const ${modelName}Relations = relations(${getModelName(

and maybe export new @better-auth/drizzle-adapter/relations-v2 here too?

export * from "@better-auth/drizzle-adapter";

@ping-maxwell
Copy link
Copy Markdown
Member Author

yes. @adamkasper
please read: #6766 (comment)

note that the defineRelationsPart is not supported in the CLI yet

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Feb 26, 2026

CLA assistant check
All committers have signed the CLA.

@gustavovalverde gustavovalverde added database Database layer, adapters, schema, migrations docs Documentation, demos devtools CLI, OpenAPI, telemetry, i18n, test-utils enhancement New feature or improvement and removed enhancement New feature or improvement labels Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

database Database layer, adapters, schema, migrations devtools CLI, OpenAPI, telemetry, i18n, test-utils docs Documentation, demos

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update Drizzle ORM Adapter to support the new Drizzle ORM query syntax (drizzle-orm v1.0.0)

10 participants