Auto-incrementing from custom value in prisma + postgresql

Viewed 2190

I'm using a prismia db client with postgresql and I'd like to start auto incrementing an integer field from 0 instead of 1. In other words, how can I write a model so that it starts from 0?

Here's the modal I have.

model SortableItem {
  id    String @id @default(uuid())
  name  String
  order Int @default(autoincrement())
}

With this implementation, when a record is inserted for the first time, the order starts from 1, but I'd like it to start from 0.

I know postgresql has RESTART to achieve this, but I couldn't find anything equivalent for prisma ORM syntax.

ALTER SEQUENCE tablename_columnname_seq RESTART WITH 0;
1 Answers

It's not possible from the schema directly but you can add this to your migration SQL and it should work:

  1. Create a migration using prisma migrate dev --create-only.

  2. Edit the generated .sql file and add the above statement.

  3. Run prisma migrate dev.

The following steps will alter the sequence.

Related