Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Error thrown when trying to insert an array of new rows using generatedAlwaysAsIdentity() for the id column #2849

Closed
elewin opened this issue Aug 27, 2024 · 1 comment
Labels
bug Something isn't working priority Will be worked on next qb/crud

Comments

@elewin
Copy link

elewin commented Aug 27, 2024

What version of drizzle-orm are you using?

0.33.0

What version of drizzle-kit are you using?

0.24.0

Describe the Bug

I have a simple schema file:

import { pgTable, integer, text } from "drizzle-orm/pg-core";
import { InferSelectModel, InferInsertModel } from "drizzle-orm";

export const test = pgTable("test", {
  id: integer("id").primaryKey().generatedAlwaysAsIdentity(),
  name: text("name").notNull(),
});

However, when I try to run a simple migration, such as:

import { Pool } from "pg";
import { drizzle } from "drizzle-orm/node-postgres";
import { test } from "../test";

const pool = new Pool({
   connectionString: "myConnectionString"
});

const db = drizzle(pool);

async function main() {
  await db
    .insert(test)
    .values([
      { name: "item 1" },
      { name: "item 2" },
      { name: "item 3" },
    ]);
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});

I get the following error:

error: cannot insert into column "id"
    at Parser.parseErrorMessage (D:\repos\testapp\server\node_modules\pg-protocol\src\parser.ts:357:11)
    at Parser.handlePacket (D:\repos\testapp\server\node_modules\pg-protocol\src\parser.ts:186:21)
    at Parser.parse (D:\repos\testapp\server\node_modules\pg-protocol\src\parser.ts:101:30)
    at Socket.<anonymous> (D:\repos\testapp\server\node_modules\pg-protocol\src\index.ts:7:48)
    at Socket.emit (node:events:518:28)
    at addChunk (node:internal/streams/readable:559:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
    at Readable.push (node:internal/streams/readable:390:5)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
  length: 272,
  severity: 'ERROR',
  code: '428C9',
  detail: 'Column "id" is an identity column defined as GENERATED ALWAYS.',
  hint: 'Use OVERRIDING SYSTEM VALUE to override.',
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'd:\\pginstaller_13.auto\\postgres.windows-x64\\src\\backend\\rewrite\\rewritehandler.c',
  line: '820',
  routine: 'rewriteTargetListIU'

If I use type serial instead of integer with .generatedAlwaysAsIdentity(), then it will work. How can this work using .generatedAlwaysAsIdentity() as the ID instead of serial?

Edit: I have noticed that it will work with integer and generatedAlwaysAsIdentity() if I insert only a single record instead of an array of records, such as:

 await db.insert(test).values({ name: "item 1" });

Though if I define the id in the schema as

id: serial("id").primaryKey()

then try to insert an array of records such as

await db
    .insert(test)
    .values([
      { name: "item 1" },
      { name: "item 2" },
      { name: "item 3" },
    ]);

it works just fine. Is the inability for an array of records to insert using integer("id").primaryKey().generatedAlwaysAsIdentity() instead of serial("id").primaryKey() a bug?

Expected behavior

The records should insert in to the table using the generated ID, when inserting an array of records

Environment & setup

Node v20.11.2
Windows 11

@elewin elewin added the bug Something isn't working label Aug 27, 2024
@elewin elewin changed the title [BUG]: 'Column "id" is an identity column defined as GENERATED ALWAYS' when trying to insert a new record [BUG]: 'Column "id" is an identity column defined as GENERATED ALWAYS' when trying to insert an array of new rows using generatedAlwaysAsIdentity() Aug 27, 2024
@elewin elewin changed the title [BUG]: 'Column "id" is an identity column defined as GENERATED ALWAYS' when trying to insert an array of new rows using generatedAlwaysAsIdentity() [BUG]: Error thrown when trying to insert an array of new rows using generatedAlwaysAsIdentity() for the id column Aug 27, 2024
@github-staff github-staff deleted a comment from vineet1401 Oct 23, 2024
@L-Mario564 L-Mario564 added priority Will be worked on next qb/crud labels Oct 24, 2024
@AndriiSherman
Copy link
Member

was fixed in drizzle-orm@0.36.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priority Will be worked on next qb/crud
Projects
None yet
Development

No branches or pull requests

3 participants