0

The id parameter of the model I designed is autoincrement, but when adding a new record, I get a unique constraint error.

Model

model InventoryItem {
  id       Int @id @default(autoincrement())
  level    Int @default(0)
  position Int
  window   Int

  character   Character @relation(fields: [characterId], references: [id])
  characterId Int
  item        Item      @relation(fields: [itemId], references: [id])
  itemId      Int
}

Code

await prisma.inventoryItem.create({
    data: {
        characterId: myCharacter.id,
        position: 0,
        level: item.level,
        itemId: item.id,
        window: 1
    }
})

prisma version: 5.2.0 @prisma/client version: 5.2.0

  • What's in your database? – Alex Wayne Sep 01 '23 at 21:03
  • what exactly do you mean? – Onur Can Yavaş Sep 01 '23 at 22:51
  • I mean is the `InventoryItem` table empty? Or does it have records in it? Perhaps auto increment counter is set below the record with the highest id and you may need to reset it. See https://stackoverflow.com/questions/5342440/reset-auto-increment-counter-in-postgres – Alex Wayne Sep 01 '23 at 22:55
  • no, I didn't set the auto increment counter afterwards. There is data in the InventoryItems table. I forgot to mention; This issue doesn't usually occur, but it's an annoying thing that does happen sometimes. – Onur Can Yavaş Sep 01 '23 at 23:01

0 Answers0