Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-incrementing from custom value in prisma + postgresql

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;
like image 892
CookieEater Avatar asked Jun 17 '26 11:06

CookieEater


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.

like image 91
Ryan Avatar answered Jun 20 '26 07:06

Ryan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!