Skip to content

Conversation

@SebastianThiebaud
Copy link

Title

Fix: Use getModelIdProperty in getRawData instead of hardcoded 'id'

Type of Change

  • New feature
  • Bug fix
  • Documentation update
  • Example update
  • Refactoring
  • Hotfix
  • Security patch
  • UI/UX improvement

Issue

The getRawData function hardcodes the field name "id" when querying Prisma, causing errors for models that use a different primary key field (e.g., uuid).

Description

Error Example

Unknown argument `id`. Did you mean `OR`?
Invalid `prisma[resource].findMany()` invocation

This occurs when:

  • A model uses uuid String @id instead of id Int @id
  • An action has a canExecute function
  • NextAdmin tries to fetch items to check canExecute

Root Cause

In utils/prisma.ts, getRawData hardcodes id:

const data = await prisma[resource].findMany({
  where: {
    id: {  // ❌ Hardcoded
      in: resourceIds
    }
  },
  include
});

However, the JSON schema correctly marks the primary key (e.g., uuid has __nextadmin.primaryKey: true), and other functions in the same file correctly use getModelIdProperty(resource).

Solution

Use getModelIdProperty(resource) to get the correct primary key field name:

const idProperty = getModelIdProperty(resource);
const data = await prisma[resource].findMany({
  where: {
    [idProperty]: {  // ✅ Dynamic property name
      in: resourceIds
    }
  },
  include
});

Changes

  • Modified getRawData to use getModelIdProperty(resource) instead of hardcoded "id"
  • This makes it consistent with other functions in the same file (e.g., preparePrismaListRequest, getMappedDataList)

Testing

Tested with a model using uuid as primary key:

  • ✅ JSON schema correctly marks uuid with __nextadmin.primaryKey: true
  • getModelIdProperty("ShowReport") correctly returns "uuid"
  • ✅ Fix allows canExecute to work properly

Impact

Positive Changes

  • Fixes bug: Actions with canExecute now work correctly for models using non-id primary keys (e.g., uuid)
  • Consistency: Makes getRawData consistent with other functions in the same file that already use getModelIdProperty(resource)
  • No breaking changes: Still defaults to "id" if no primary key is found in the schema, maintaining backward compatibility
  • No performance impact: Adds only one function call (getModelIdProperty), which is already used extensively throughout the codebase
  • No new dependencies: Uses existing getModelIdProperty function from utils/server.mjs

Behavioral Changes

  • getRawData will now correctly query models that use uuid, customId, or any other primary key field name
  • Actions with canExecute functions will no longer throw Prisma validation errors for such models

Testing Recommendations

  • Test with models using id Int @id (existing behavior should remain unchanged)
  • Test with models using uuid String @id (new behavior should work correctly)
  • Test with models using composite primary keys (should still default to "id" if no __nextadmin.primaryKey is found)

Additional Information

This is consistent with how other functions in the file handle primary keys:

  • Line ~143: const idProperty = getModelIdProperty(resource);
  • Line ~264: const idProperty = getModelIdProperty(resource);
  • Line ~278: Uses [idProperty] in queries

@changeset-bot
Copy link

changeset-bot bot commented Dec 16, 2025

🦋 Changeset detected

Latest commit: d47b50b

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

This PR includes changesets to release 1 package
Name Type
@premieroctet/next-admin 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

@foyarash
Copy link
Collaborator

Hi @SebastianThiebaud thanks for the PR, could you please add a changeset by running the pnpm changeset command at the root of the project ?

Thank you

@SebastianThiebaud
Copy link
Author

Hi @SebastianThiebaud thanks for the PR, could you please add a changeset by running the pnpm changeset command at the root of the project ?

Thank you

Done!

@foyarash
Copy link
Collaborator

Thank you

Could you have a look at the failing steps of the CI ?

@foyarash
Copy link
Collaborator

@SebastianThiebaud can you please rebase the main branch ? The tests should be fixed now

@SebastianThiebaud
Copy link
Author

Done

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