Fix: Use dynamic ID property in prisma query for resource filtering #653
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Title
Fix: Use getModelIdProperty in getRawData instead of hardcoded 'id'
Type of Change
Issue
The
getRawDatafunction 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
This occurs when:
uuid String @idinstead ofid Int @idcanExecutefunctioncanExecuteRoot Cause
In
utils/prisma.ts,getRawDatahardcodesid:However, the JSON schema correctly marks the primary key (e.g.,
uuidhas__nextadmin.primaryKey: true), and other functions in the same file correctly usegetModelIdProperty(resource).Solution
Use
getModelIdProperty(resource)to get the correct primary key field name:Changes
getRawDatato usegetModelIdProperty(resource)instead of hardcoded"id"preparePrismaListRequest,getMappedDataList)Testing
Tested with a model using
uuidas primary key:uuidwith__nextadmin.primaryKey: truegetModelIdProperty("ShowReport")correctly returns"uuid"canExecuteto work properlyImpact
Positive Changes
canExecutenow work correctly for models using non-idprimary keys (e.g.,uuid)getRawDataconsistent with other functions in the same file that already usegetModelIdProperty(resource)"id"if no primary key is found in the schema, maintaining backward compatibilitygetModelIdProperty), which is already used extensively throughout the codebasegetModelIdPropertyfunction fromutils/server.mjsBehavioral Changes
getRawDatawill now correctly query models that useuuid,customId, or any other primary key field namecanExecutefunctions will no longer throw Prisma validation errors for such modelsTesting Recommendations
id Int @id(existing behavior should remain unchanged)uuid String @id(new behavior should work correctly)"id"if no__nextadmin.primaryKeyis found)Additional Information
This is consistent with how other functions in the file handle primary keys:
const idProperty = getModelIdProperty(resource);const idProperty = getModelIdProperty(resource);[idProperty]in queries