Skip to main content

One post tagged with "prisma"

View All Tags

Auto-increment field with Prisma

· 2 min read

SELECT setval(pg_get_serial_sequence('tablename', 'id'), coalesce(max(id)+1, 1), false) FROM "tablename";

This post is a knowledgebase article on Prisma with PostgreSQL.

The typical model in a Prisma schema has an id field that auto-increments:

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

The following is working (most of the time) code that inserts a new entry:

prisma.user.create({
data: { name: 'Some User' },
});

However, the following error may occur:

Unique constraint failed on the fields: (id)

This is baffling - if the field auto-increments, how can the unique constraint be violated?