drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
24.61k stars 649 forks source link

[BUG]: postgres insert and generatedAlwaysAsIdentity tries to insert null value #3576

Open ChxGuillaume opened 3 days ago

ChxGuillaume commented 3 days ago

Report hasn't been filed before.

What version of drizzle-orm are you using?

0.36.3

What version of drizzle-kit are you using?

not installed

Other packages

No response

Describe the Bug

When using a model like this

export const materials = pgTable("materials", {
  id: integer().primaryKey().generatedAlwaysAsIdentity(),
  name: varchar({ length: 255 }),]
});

and executing

drizzleDb
    .insert(materials)
    .values({
        name: "my name"
    })
    .returning();

The generated SQL will is

INSERT INTO "materials" ("id", "name")
VALUES (null, 'my name')

It's trying to set the ID to null just errors, thus making the insert impossible

[2024-11-18 22:43:29] [23502] ERROR: null value in column "id" of relation "materials" violates not-null constraint

I am pretty sure that the Primary Column shouldn't get an insert value

Note: I am using Postgres and tried with version 11 and 16 of postgres