[GH-ISSUE #7271] [Bug] Drizzle adapter drops OR clauses when mixed with AND connectors in where conditions #10776

Open
opened 2026-04-13 07:07:29 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @Ridhim-RR on GitHub (Jan 11, 2026).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/7271

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Description

The Drizzle adapter has a bug where queries with mixed AND/OR connectors in where clauses will drop one of the connector groups, leading to incorrect query results.

Current vs. Expected behavior

Current Behavior

When using findMany or findOne with a where clause that contains both AND and OR connectors, only the first connector group is applied. The second group is silently ignored.

Expected Behavior

Both AND and OR connector groups should be combined properly, resulting in queries like:

sql
WHERE (condition1 AND condition2) AND (condition3 OR condition4)

import { drizzleAdapter } from 'better-auth/adapters/drizzle';

// Create users with different attributes
await adapter.create({ 
  model: "user", import { drizzleAdapter } from 'better-auth/adapters/drizzle';

// Create users with different attributes
await adapter.create({ 
  model: "user", 
  data: { email: "admin@company.com", name: "Admin User" } 
});
await adapter.create({ 
  model: "user", 
  data: { email: "other@company.com", name: "Other Person" } 
});

// Query with mixed connectors
const result = await adapter.findMany({
  model: "user",
  where: [
    { field: "email", value: "company.com", operator: "contains" }, // implicit AND
    { field: "name", value: "Admin", operator: "contains", connector: "OR" },
  ],
});

// Expected: 2 users (both @company.com users match)
// Actual: 1 user (only the AND condition is applied, OR is dropped)
  data: { email: "admin@company.com", name: "Admin User" } 
});
await adapter.create({ 
  model: "user", 
  data: { email: "other@company.com", name: "Other Person" } 
});

// Query with mixed connectors
const result = await adapter.findMany({
  model: "user",
  where: [
    { field: "email", value: "company.com", operator: "contains" }, // implicit AND
    { field: "name", value: "Admin", operator: "contains", connector: "OR" },
  ],
});

// Expected: 2 users (both @company.com users match)
// Actual: 1 user (only the AND condition is applied, OR is dropped)

Root Cause
In ,drizzle-adapter.ts, the convertWhereClause function builds both andClause and orClause but return them as array [andClause, orClause].
The calling code then uses only clause[0] , effectively dropping the second clause:

let query = db.query[model].findFirst({
  where: clause[0],  // Only uses first element!
  with: includes,
});

Impact

  • Affects all Drizzle users (PostgreSQL, MySQL, SQLite)
  • Queries with mixed AND/OR conditions return incorrect results
  • Silent failure - no error is thrown

What version of Better Auth are you using?

1.5.0-beta.3

System info

Operating System:

Ubuntu 24.04.3 LTS
Kernel: 6.14.0-37-generic
Architecture: x86_64
Development Environment:

Node.js: v24.5.0
pnpm: 10.26.2
Git: 2.43.0

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)


Additional context

No response

Originally created by @Ridhim-RR on GitHub (Jan 11, 2026). Original GitHub issue: https://github.com/better-auth/better-auth/issues/7271 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce ## Description The Drizzle adapter has a bug where queries with mixed AND/OR connectors in `where` clauses will drop one of the connector groups, leading to incorrect query results. ### Current vs. Expected behavior ## Current Behavior When using `findMany` or `findOne` with a `where` clause that contains both AND and OR connectors, only the first connector group is applied. The second group is silently ignored. ## Expected Behavior Both AND and OR connector groups should be combined properly, resulting in queries like: ``` sql WHERE (condition1 AND condition2) AND (condition3 OR condition4) import { drizzleAdapter } from 'better-auth/adapters/drizzle'; // Create users with different attributes await adapter.create({ model: "user", import { drizzleAdapter } from 'better-auth/adapters/drizzle'; // Create users with different attributes await adapter.create({ model: "user", data: { email: "admin@company.com", name: "Admin User" } }); await adapter.create({ model: "user", data: { email: "other@company.com", name: "Other Person" } }); // Query with mixed connectors const result = await adapter.findMany({ model: "user", where: [ { field: "email", value: "company.com", operator: "contains" }, // implicit AND { field: "name", value: "Admin", operator: "contains", connector: "OR" }, ], }); // Expected: 2 users (both @company.com users match) // Actual: 1 user (only the AND condition is applied, OR is dropped) data: { email: "admin@company.com", name: "Admin User" } }); await adapter.create({ model: "user", data: { email: "other@company.com", name: "Other Person" } }); // Query with mixed connectors const result = await adapter.findMany({ model: "user", where: [ { field: "email", value: "company.com", operator: "contains" }, // implicit AND { field: "name", value: "Admin", operator: "contains", connector: "OR" }, ], }); // Expected: 2 users (both @company.com users match) // Actual: 1 user (only the AND condition is applied, OR is dropped) ``` **Root Cause** In ,drizzle-adapter.ts, the convertWhereClause function builds both andClause and orClause but return them as array [andClause, orClause]. The calling code then uses only clause[0] , effectively dropping the second clause: ``` let query = db.query[model].findFirst({ where: clause[0], // Only uses first element! with: includes, }); ``` **Impact** - Affects all Drizzle users (PostgreSQL, MySQL, SQLite) - Queries with mixed AND/OR conditions return incorrect results - Silent failure - no error is thrown ### What version of Better Auth are you using? 1.5.0-beta.3 ### System info ```bash Operating System: Ubuntu 24.04.3 LTS Kernel: 6.14.0-37-generic Architecture: x86_64 Development Environment: Node.js: v24.5.0 pnpm: 10.26.2 Git: 2.43.0 ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript ``` ### Additional context _No response_
GiteaMirror added the databasebug labels 2026-04-13 07:07:29 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Jan 11, 2026):

It seems you've identified a bug with the Drizzle adapter where mixed AND/OR connectors in where clauses drop one connector group. This behavior has indeed been reported, leading to incorrect query results where only one group is applied while the other is ignored. Specifically, queries constructed like WHERE (condition1 AND condition2) AND (condition3 OR condition4) should return results satisfying both conditions.

Currently, there is an open discussion regarding this issue, and it is known to affect users of Drizzle with PostgreSQL, MySQL, and SQLite. The core problem lies in how the convertWhereClause function processes these clauses, only returning the first element from an array of clauses created during the query preparation.

To report this bug properly, please ensure to include the specific versions you are using and any relevant query structures that demonstrate the issue, as you've done in your description.

As a next step, you may want to tag @better-auth-agent for further assistance. Is there anything specific you would like to know about best practices when using the Drizzle adapter?

If you need more help, tag @better-auth-agent in a comment so I can respond.

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3735567562 --> @better-auth-agent[bot] commented on GitHub (Jan 11, 2026): It seems you've identified a bug with the Drizzle adapter where mixed AND/OR connectors in `where` clauses drop one connector group. This behavior has indeed been reported, leading to incorrect query results where only one group is applied while the other is ignored. Specifically, queries constructed like `WHERE (condition1 AND condition2) AND (condition3 OR condition4)` should return results satisfying both conditions. Currently, there is an open discussion regarding this issue, and it is known to affect users of Drizzle with PostgreSQL, MySQL, and SQLite. The core problem lies in how the `convertWhereClause` function processes these clauses, only returning the first element from an array of clauses created during the query preparation. To report this bug properly, please ensure to include the specific versions you are using and any relevant query structures that demonstrate the issue, as you've done in your description. As a next step, you may want to tag @better-auth-agent for further assistance. Is there anything specific you would like to know about best practices when using the Drizzle adapter? _If you need more help, tag @better-auth-agent in a comment so I can respond._ <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) • [Discord](https://discord.gg/fG2XXEuQX3) • [GitHub](https://github.com/Skyvern-AI/Skyvern) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/fG2XXEuQX3) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/Skyvern-AI/Skyvern)
Author
Owner

@better-auth-agent[bot] commented on GitHub (Jan 11, 2026):

It looks like you encountered a known issue with the Drizzle adapter in Better Auth where mixed AND/OR conditions in where clauses can lead to incorrect query results, specifically dropping one of the connector groups. This issue is recognized and was addressed in the past with a fix that ensures all supported operators in multiple where conditions are handled correctly, resulting in expected query behavior. However, it seems there might be additional debugging needed for specific conditions to ensure functionality across all use cases [1], [3].

To resolve this:

  1. Ensure that you’re using the latest version of Better Auth (at least v1.5.0) as some fixes were introduced recently.
  2. You may want to consider submitting a detailed report with specifics of your queries in the GitHub repository or checking if there's an ongoing discussion about it.

If you need further assistance or if this issue persists, please tag @bekacru or @ping__ in your inquiry, and they will be able to help you more effectively!

What specific queries are you trying to implement, and how are they structured?

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3735569923 --> @better-auth-agent[bot] commented on GitHub (Jan 11, 2026): It looks like you encountered a known issue with the Drizzle adapter in Better Auth where mixed AND/OR conditions in `where` clauses can lead to incorrect query results, specifically dropping one of the connector groups. This issue is recognized and was addressed in the past with a fix that ensures all supported operators in multiple `where` conditions are handled correctly, resulting in expected query behavior. However, it seems there might be additional debugging needed for specific conditions to ensure functionality across all use cases [[1]](https://github.com/better-auth/better-auth/pull/6168), [[3]](https://github.com/better-auth/better-auth/pull/5311). To resolve this: 1. Ensure that you’re using the latest version of Better Auth (at least v1.5.0) as some fixes were introduced recently. 2. You may want to consider submitting a detailed report with specifics of your queries in the GitHub repository or checking if there's an ongoing discussion about it. If you need further assistance or if this issue persists, please tag @bekacru or @ping__ in your inquiry, and they will be able to help you more effectively! What specific queries are you trying to implement, and how are they structured? <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) • [Discord](https://discord.gg/fG2XXEuQX3) • [GitHub](https://github.com/Skyvern-AI/Skyvern) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/fG2XXEuQX3) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/Skyvern-AI/Skyvern)
Author
Owner

@Ridhim-RR commented on GitHub (Jan 12, 2026):

@himself65 can you look into this?

<!-- gh-comment-id:3737483236 --> @Ridhim-RR commented on GitHub (Jan 12, 2026): @himself65 can you look into this?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#10776