Skip to content

[FEATURE] - Support custom primary key field instead of id in NextAdminOptions #666

@helloitsmix

Description

@helloitsmix

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions