drizzle adapter setActive organization results wrong session update #639

Closed
opened 2026-03-13 07:58:11 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @gertmosin on GitHub (Feb 7, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. create NextJS app.
  2. install better-auth
  3. use better-auth documentation on using drizzle adapter
  4. enable organization plugin
  5. create organization
  6. set active organization on client side

in better-auth/packages/better-auth/src/adapters/drizzle-adapter/drizzle-adapter.ts

withReturning: async (
			model: string,
			builder: any,
			data: Record<string, any>,
		) => {
			if (config.provider !== "mysql") {
				const c = await builder.returning();
				return c[0];
			}
			const result = await builder.execute();
			const updatedResult = builder.config?.where;
			const builderVal = builder.config?.values;
			if (updatedResult) {
				const upId = updatedResult?.queryChunks[3]?.value;
				const schemaModel = getSchema(model);
				const res = await db
					.select()
					.from(schemaModel)
					.where(eq(schemaModel.id, upId));
				return res[0];
			} else if (builderVal) {
				const tId = builderVal[0]?.id.value;
				const schemaModel = getSchema(model);
				const res = await db
					.select()
					.from(schemaModel)
					.where(eq(schemaModel.id, tId));
				return res[0];
			} else if (data.id) {
				const schemaModel = getSchema(model);
				const res = await db
					.select()
					.from(schemaModel)
					.where(eq(schemaModel.id, data.id));
				return res[0];
			}
		}

### Current vs. Expected behavior

Current:

With drizzle my query for updating the active organization
``` authClient.organization.setActive({ organizationId: org.id })```
 this upId field is token field but 

```tsx 
const res = await db.select().from(schemaModel).where(eq(schemaModel.id, upId));

schemaModel.id tries to find session.id but upId is sessionToken so the setActive organization will result an error 500

Image

Expected:

POST status 200

My current solution was to change the schemaModel.id to schemaModel[queryfield] and instead use the field provided in the where clause to update the schema.

      if (updatedResult) {
        const upId = updatedResult?.queryChunks[3]?.value;
        const queryField = updatedResult?.queryChunks[1]?.name
        const schemaModel = getSchema(model);
        const res = await db.select().from(schemaModel).where(eq(schemaModel[queryField], upId))
        return res[0];
      }

I have no idea if this option is even viable or maybe i am just dumb and missed something and its not a problem.

What version of Better Auth are you using?

1.1.16

Provide environment information

- Node: 18.20.4
- OS: W11
- Browser: Firefox/134.0

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

Package

Auth config (if applicable)

import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { nextCookies } from "better-auth/next-js";
import { admin, organization } from "better-auth/plugins";
import { db } from "~/server/db";

export const auth = betterAuth({
  database: drizzleAdapter(db, {
    provider: "mysql"
  }),
  plugins: [
    organization(),
    admin(),
    nextCookies(),
  ],
});

Additional context

No response

Originally created by @gertmosin on GitHub (Feb 7, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. create NextJS app. 2. install better-auth 3. use better-auth documentation on using drizzle adapter 4. enable organization plugin 5. create organization 6. set active organization on client side in better-auth/packages/better-auth/src/adapters/drizzle-adapter/drizzle-adapter.ts ```tsx withReturning: async ( model: string, builder: any, data: Record<string, any>, ) => { if (config.provider !== "mysql") { const c = await builder.returning(); return c[0]; } const result = await builder.execute(); const updatedResult = builder.config?.where; const builderVal = builder.config?.values; if (updatedResult) { const upId = updatedResult?.queryChunks[3]?.value; const schemaModel = getSchema(model); const res = await db .select() .from(schemaModel) .where(eq(schemaModel.id, upId)); return res[0]; } else if (builderVal) { const tId = builderVal[0]?.id.value; const schemaModel = getSchema(model); const res = await db .select() .from(schemaModel) .where(eq(schemaModel.id, tId)); return res[0]; } else if (data.id) { const schemaModel = getSchema(model); const res = await db .select() .from(schemaModel) .where(eq(schemaModel.id, data.id)); return res[0]; } } ### Current vs. Expected behavior Current: With drizzle my query for updating the active organization ``` authClient.organization.setActive({ organizationId: org.id })``` this upId field is token field but ```tsx const res = await db.select().from(schemaModel).where(eq(schemaModel.id, upId)); ``` schemaModel.id tries to find session.id but upId is sessionToken so the setActive organization will result an error 500 ![Image](https://github.com/user-attachments/assets/a39f5c3c-57ef-4f68-a287-cda3cac6d7e2) Expected: POST status 200 My current solution was to change the schemaModel.id to schemaModel[queryfield] and instead use the field provided in the where clause to update the schema. ```tsx if (updatedResult) { const upId = updatedResult?.queryChunks[3]?.value; const queryField = updatedResult?.queryChunks[1]?.name const schemaModel = getSchema(model); const res = await db.select().from(schemaModel).where(eq(schemaModel[queryField], upId)) return res[0]; } ``` I have no idea if this option is even viable or maybe i am just dumb and missed something and its not a problem. ### What version of Better Auth are you using? 1.1.16 ### Provide environment information ```bash - Node: 18.20.4 - OS: W11 - Browser: Firefox/134.0 ``` ### Which area(s) are affected? (Select all that apply) Package ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth"; import { drizzleAdapter } from "better-auth/adapters/drizzle"; import { nextCookies } from "better-auth/next-js"; import { admin, organization } from "better-auth/plugins"; import { db } from "~/server/db"; export const auth = betterAuth({ database: drizzleAdapter(db, { provider: "mysql" }), plugins: [ organization(), admin(), nextCookies(), ], }); ``` ### Additional context _No response_
GiteaMirror added the bug label 2026-03-13 07:58:11 -05:00
Author
Owner

@Bekacru commented on GitHub (Feb 8, 2025):

should be fixed on v1.1.17-beta.3. feel free to reopen if not.

@Bekacru commented on GitHub (Feb 8, 2025): should be fixed on `v1.1.17-beta.3`. feel free to reopen if not.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#639