0

I am using Prisma with a Node (TS) application. I have a table which is defined in schema as follows:

    model User {
      id            Int                @id @default(autoincrement())
      createdAt     DateTime           @default(now())
      updatedAt     DateTime           @updatedAt
      email         String             @db.VarChar(255) @unique
      name          String             @db.VarChar(255)
      password      String             @db.VarChar(255)
    }

I have created migrations, deployed them to the database, created a client module also. Once the migration was deployed, I could see the table created in the database. I added some rows manually to the database. (without Prisma Client). Let us say, I add rows with id = 1, id = 2 and id = 3.

Now when I try to add a row using Prisma Client, it fails to create an entry in the DB. The error mentions that it can't create an entry because the id is conflicting. I am assuming it is trying to make an id = 1 entry since the client doesn't seem to have identified the existing last id.

How can we work around this? I don't want to delete those rows.

  • 1
    `PostgreSQL` has an internal sequence counter that is updated every time a new row is (correctly) created. You must set the counter to a _new_ correct value (_max_ `id` + 1). From [this answer](https://stackoverflow.com/a/5342503/6676781) you have to execute `ALTER SEQUENCE user_id_seq RESTART WITH 4`. – Carlo Corradini Sep 03 '22 at 15:10
  • Thanks for your answer! I did find the answer that you've linked but I was interested in knowing if there is any way to feed this in schema or an automated way. Running the query does seem a solution though! – Lohit Marodia Sep 04 '22 at 13:55
  • What you are searching for is called a __Migration__. See [Migration docs](https://www.prisma.io/docs/concepts/components/prisma-migrate) for more information. – Carlo Corradini Sep 04 '22 at 13:57

0 Answers0