0

Assuming the following data model


model Card {
  id   Int  @id @default(autoincrement())
  name String
}

Let's then say I have in the Card table the following

id: 0, name: "0"
id: 1, name: "1"

For some reason, let's say that I have to change id:1 to id:2.

id: 0, name: "0"
id: 2, name: "1"

If I then invoke prisma.card.create(<some info here>...), I will get an error like this:

Unhandled Rejection (Error): GraphQL error: 
Invalid `prisma.card.create()` invocation:
  Unique constraint failed on the fields: (`id`)

Which makes perfect sense. However, is there a way I can make autoincrement skip an existing id? That is, skip to 3 such that:

id: 0, name: "0"
id: 2, name: "1"
id: 3, name: "3"
fingia
  • 504
  • 7
  • 20

1 Answers1

0

The autoincrement() function in Prisma is translated directly to a native behavior of the underlying database. So, your question should be more specific about your database rather than about Prisma.

With that being said, even on the DB level, it is rarely recommended to change the seed of the ID in a table.

There is a DDL command to reset the id of a table but you should not use that as part of a normal lifecycle of your application.

For example, this is how to do it on postgres - Reset auto increment counter in postgres

yuvalhazaz
  • 80
  • 2