[PR #5036] [MERGED] fix(adapter): foreign keys that are nullable on number ids can return string of null #13977

Closed
opened 2026-04-13 09:14:52 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5036
Author: @ping-maxwell
Created: 10/2/2025
Status: Merged
Merged: 10/2/2025
Merged by: @himself65

Base: canaryHead: fix/number-id-foreign-key-null-returns-string


📝 Commits (6)

  • fea9a22 fix(adapter): foreign keys that are nullable on number ids can return string of null
  • 4ca0875 chore: lint
  • 7ea0110 fix: kysely + pg + useNumberId causes FKs to be non-nullable
  • 080939d chore: lint
  • f7c4ea5 fix: mongo didnt allow nullable FKs
  • f559683 chore: cleanup

📊 Changes

5 files changed (+119 additions, -4 deletions)

View changed files

📝 packages/better-auth/src/adapters/adapter-factory/index.ts (+2 -2)
📝 packages/better-auth/src/adapters/adapter-factory/test/adapter-factory.test.ts (+67 -0)
📝 packages/better-auth/src/adapters/mongodb-adapter/mongodb-adapter.ts (+3 -0)
📝 packages/better-auth/src/adapters/tests/normal.ts (+32 -1)
📝 packages/better-auth/src/db/get-migration.ts (+15 -1)

📄 Description

setting null on a foreign key will return a stringified null version when using findOne or findMany if useNumberId is enabled.

  • fixed
  • tests

Kysely + useNumberId + pg doesn't allow null on specifically specified nullable foreign key fields.

This was caused by our schema generator generating serial in this specific situation, for primary keys this is fine, but for foreign keys this should just be an integer.

  • fixed
  • tests

MongoDB Adapter didn't support null on nullable foreign key fields.

  • fixed
  • tests

Closes https://github.com/better-auth/better-auth/issues/3001


Summary by cubic

Fixes nullable foreign key casting when useNumberId is enabled. Null values now stay null (not the string "null") for both single fields and arrays.

  • Bug Fixes
    • Cast number IDs only when value is not null; array values checked element-by-element.
    • Added tests covering nullable foreign keys with and without useNumberId in adapter-factory and normal suites.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/5036 **Author:** [@ping-maxwell](https://github.com/ping-maxwell) **Created:** 10/2/2025 **Status:** ✅ Merged **Merged:** 10/2/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix/number-id-foreign-key-null-returns-string` --- ### 📝 Commits (6) - [`fea9a22`](https://github.com/better-auth/better-auth/commit/fea9a22b46a417c9b4096c69b7e5fd4aa7b53b2c) fix(adapter): foreign keys that are nullable on number ids can return string of `null` - [`4ca0875`](https://github.com/better-auth/better-auth/commit/4ca08751ff9b2b8fd5457c0151c3ff72d91c87aa) chore: lint - [`7ea0110`](https://github.com/better-auth/better-auth/commit/7ea01107bcee682a85692d22ba355a80e7aa3ec1) fix: kysely + pg + useNumberId causes FKs to be non-nullable - [`080939d`](https://github.com/better-auth/better-auth/commit/080939d4db74abca106a88a249f2d21d0cc3a61c) chore: lint - [`f7c4ea5`](https://github.com/better-auth/better-auth/commit/f7c4ea547559ec6a4afe31ed5e7ec16a657a9251) fix: mongo didnt allow nullable FKs - [`f559683`](https://github.com/better-auth/better-auth/commit/f55968389630e58822a8bfc530e8b1c5f265c2d8) chore: cleanup ### 📊 Changes **5 files changed** (+119 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/adapters/adapter-factory/index.ts` (+2 -2) 📝 `packages/better-auth/src/adapters/adapter-factory/test/adapter-factory.test.ts` (+67 -0) 📝 `packages/better-auth/src/adapters/mongodb-adapter/mongodb-adapter.ts` (+3 -0) 📝 `packages/better-auth/src/adapters/tests/normal.ts` (+32 -1) 📝 `packages/better-auth/src/db/get-migration.ts` (+15 -1) </details> ### 📄 Description setting `null` on a foreign key will return a stringified null version when using `findOne` or `findMany` if `useNumberId` is enabled. - [x] fixed - [x] tests Kysely + `useNumberId` + `pg` doesn't allow `null` on specifically specified `nullable` foreign key fields. > This was caused by our schema generator generating `serial` in this specific situation, for primary keys this is fine, but for foreign keys this should just be an `integer`. - [x] fixed - [x] tests MongoDB Adapter didn't support `null` on nullable foreign key fields. - [x] fixed - [x] tests Closes https://github.com/better-auth/better-auth/issues/3001 <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes nullable foreign key casting when useNumberId is enabled. Null values now stay null (not the string "null") for both single fields and arrays. - **Bug Fixes** - Cast number IDs only when value is not null; array values checked element-by-element. - Added tests covering nullable foreign keys with and without useNumberId in adapter-factory and normal suites. <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-13 09:14:52 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#13977