drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: make empty insert default to insert all default values if all columns have one

Open
#1,629 0 comments 1 reaction 0 assignees View on GitHub
improvement qb/crud
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Describe what you want

Let's imagine alumPersonaTable has attributes which all have defaults values:
```ts
export const alumPersonaTable = pgTable("alum_persona", {
id: serial("id").primaryKey(),
// TODO: add attributes
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at").defaultNow().notNull(),
});
```
Instead of using [DEFAULT VALUES](https://www.postgresql.org/docs/current/sql-insert.html) from insert in pgsql, the following throw an error:
```ts
const insertedAlumPersona = await tx
.insert(alumPersonaTable)
.values({}) // this fails :( and .values() or values(undefined) is not permitted by the type system as well
.returning({
insertedId: alumPersonaTable.id,
});
```
Instead, I must do that:
```ts
const insertedAlumPersona = await tx
.insert(alumPersonaTable)
.values({ id: undefined }) // this is necessary
.returning({
insertedId: alumPersonaTable.id,
});
```
artificially choosing one of the value and explicitely setting it as undefined for default to be used.

Note that I know this is a very edgy case, and most likely if I don't need to insert any attribute, my table design is kinda flawed - but still, I have my reasons to do that ;)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.