Skip to content

Conversation

@thuillart
Copy link
Contributor

Problem

The isUniqueField function tries to access the Better Auth schema using Convex table names (e.g., "users", "accounts", "sessions") directly, but the schema object uses Better Auth's internal model names as keys (e.g., "user", "account", "session").

This causes the error:

Cannot read properties of undefined (reading 'fields')

Solution

The fix maps Convex table names to Better Auth internal model names by finding the key where betterAuthSchema[key].modelName === model, ensuring compatibility with custom modelName values in Better Auth configuration.

Changes

  • Updated isUniqueField function in src/adapterUtils.ts to correctly map model names
  • Added safe access with optional chaining for modelSchema?.fields

Testing

This fix is based on how @convex-dev/better-auth@0.9.7 correctly handles model name mapping, ensuring compatibility with custom modelName values in Better Auth configuration.

Fixes the issue where unique field checks fail when Better Auth schema uses different model names than Convex table names.

Fixes a bug where isUniqueField tries to access the Better Auth schema
using Convex table names (e.g., "users") directly, but the schema object
uses Better Auth's internal model names as keys (e.g., "user").

This causes the error: "Cannot read properties of undefined (reading 'fields')"

The fix maps Convex table names to Better Auth internal model names by
finding the key where betterAuthSchema[key].modelName === model, ensuring
compatibility with custom modelName values in Better Auth configuration.
@changeset-bot
Copy link

changeset-bot bot commented Nov 12, 2025

🦋 Changeset detected

Latest commit: e7c102d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
better-auth-convex Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@zbeyens zbeyens merged commit 5f026d2 into udecode:main Nov 12, 2025
@thuillart thuillart deleted the fix/isUniqueField-model-mapping branch November 12, 2025 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants