Summary
Currently, next-admin seems to assume that the primary key of each model is always named id. However, it is fairly common in Prisma schemas to use a different field name as the primary key, or to reference a unique field (other than id) in a relation.
It would be great to have a way to tell next-admin which field should be treated as the primary key for a given model — either globally or per-model via NextAdminOptions.
If there is already a way to handle this, I'd love to know — I couldn't find anything in the documentation!
Basic Example
Consider the following Prisma schema:
model Carriera {
id Int @id @default(autoincrement())
key String @unique
nome String
descrizione String?
abilitaCarriere AbilitaCarriera[]
@@map("carriere")
}
model Abilita {
id Int @id @default(autoincrement())
key String @unique
nome String
descrizione String?
abilitaCarriere AbilitaCarriera[]
@@map("abilita")
}
model AbilitaCarriera {
possibilita Int?
carriera Carriera @relation(fields: [carrieraKey], references: [key])
carrieraKey String
abilita Abilita @relation(fields: [abilitaKey], references: [key])
abilitaKey String
@@id([carrieraKey, abilitaKey])
@@map("abilita_carriere")
}
Here, AbilitaCarriera has a composite primary key (carrieraKey + abilitaKey) and the relations point to the key field (not id) of the related models. Operations on this model currently fail because next-admin looks for an id field that is not used in this context.
A possible solution could be a new option in NextAdminOptions, for example:
const options: NextAdminOptions = {
models: {
AbilitaCarriera: {
primaryKey: ['carrieraKey', 'abilitaKey'], // composite key support
},
Carriera: {
primaryKey: 'key', // custom single key
},
},
};
Drawbacks
No response
Unresolved questions
- Is there already a supported way to configure a custom or composite primary key in
next-admin? If so, could it be better documented?
- Should composite primary keys be serialized in a specific way for URL routing (e.g.,
carrieraKey__abilitaKey)?
- How should the edit/delete actions behave when the primary key is composite?
Summary
Currently,
next-adminseems to assume that the primary key of each model is always namedid. However, it is fairly common in Prisma schemas to use a different field name as the primary key, or to reference a unique field (other thanid) in a relation.It would be great to have a way to tell
next-adminwhich field should be treated as the primary key for a given model — either globally or per-model viaNextAdminOptions.If there is already a way to handle this, I'd love to know — I couldn't find anything in the documentation!
Basic Example
Consider the following Prisma schema:
Here,
AbilitaCarrierahas a composite primary key (carrieraKey+abilitaKey) and the relations point to thekeyfield (notid) of the related models. Operations on this model currently fail becausenext-adminlooks for anidfield that is not used in this context.A possible solution could be a new option in
NextAdminOptions, for example:Drawbacks
No response
Unresolved questions
next-admin? If so, could it be better documented?carrieraKey__abilitaKey)?