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
21.44k stars 484 forks source link

[BUG]: Execute Alter Sequence #2511

Open mckamyk opened 2 weeks ago

mckamyk commented 2 weeks ago

What version of drizzle-orm are you using?

0.31.2

What version of drizzle-kit are you using?

0.22.7

Describe the Bug

change the autoincrement current value using db.execute

await db.execute(sql`alter sequence posts_id_seq restart with ${newId}`)

// have also tried
await db.execute(sql`alter sequence posts_id_seq restart with cast(${newId} as bigint)`)
await db.execute(sql`alter sequence posts_id_seq restart with ${BigInt(newId)}`)

Causes the following error

1027 | replicationStart=I.portalSuspended=I.noData=I.closeComplete=I.bindComplete=I.parseComplete=
1028 | void 0;I.parseComplete={name:"parseComplete",length:5};I.bindComplete={name:"bin\
1029 | dComplete",length:5};I.closeComplete={name:"closeComplete",length:5};I.noData={name:"\
1030 | noData",length:5};I.portalSuspended={name:"portalSuspended",length:5};I.replicationStart=
1031 | {name:"replicationStart",length:4};I.emptyQuery={name:"emptyQuery",length:4};I.copyDone=
1032 | {name:"copyDone",length:4};var Dr=class Dr extends Error{constructor(e,t,n){super(
                                 ^
error: syntax error at or near "$1"
 code: "42601"

      at new Dr (/Users/redacted/node_modules/@neondatabase/serverless/index.mjs:1032:27)
      at parseErrorMessage (/Users/redacted/node_modules/@neondatabase/serverless/index.mjs:1182:12)
      at parse (/Users/redacted/node_modules/@neondatabase/serverless/index.mjs:1127:36)
      at /Users/redacted/node_modules/@neondatabase/serverless/index.mjs:397:12
      at /Users/redacted/node_modules/@neondatabase/serverless/index.mjs:988:80

Expected behavior

Expected to return nothing with no error.

The exact same query, without the parameterization works fine both in drizzle and the Neon Console

Environment & setup

Using Neon.tech postgres v16 with pooler connection

import * as schema from "./schema";
import { drizzle } from "drizzle-orm/neon-serverless";
import { neonConfig, Pool } from "@neondatabase/serverless";
import { Resource } from "sst";
import ws from "ws";

neonConfig.webSocketConstructor = ws;

const pool = new Pool({ connectionString: Resource.DbUrl.value });
export const db = drizzle(pool, { schema });

Example of the table I'm trying to update.

import {pgTable, bigserial} from 'drizzle-orm/pg-core'

export const posts  = pgTable("posts", {
  id: bigserial("id", { mode: "number" }).notNull().primaryKey(),
  // ...
});
aeons commented 1 day ago

I had something like this and using the sql.raw interpolator on the parameters worked.

Ie.

await db.execute(sql`alter sequence posts_id_seq restart with ${sql.raw(newId)}`)