Skip to main content

2 posts tagged with "knowledgebase"

View All Tags

TypeScript object types with optional and minimally required properties

· 3 min read

TL;DR

// Solution 1
type LabelEntry = { label: string };
type ImageEntry = { image: string };
type Entry = LabelEntry | ImageEntry;

// Solution 2
type Entry = Record<'label' & 'image', string>;

An object type that I find myself creating very frequently is one that has one or all of several properties. For example, I can create an entry with a text label, one with an image, or one with both:

Auto-increment field with Prisma

· 2 min read

SELECT setval(pg_get_serial_sequence(&#39;tablename&#39;, &#39;id&#39;), coalesce(max(id)+1, 1), false) FROM &quot;tablename&quot;;

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?