[BUG]: inserting json does not work when using `sql` template string

Author: camc314Created Oct 21, 2024Updated Sep 16, 2026
Labelsbugdb/postgresdriver/postgresjspriorityqb/crudhas-pr

What version of drizzle-orm are you using?

drizzle-orm

What version of drizzle-kit are you using?

No response

Describe the Bug

I tried this morning with the latest drizzle-orm version.

It appears that this broke in #1785 .

Attempting to insert a JS object using the sql template lit does not work correctly

The following error is thrown:

The "string" argument must be of type string or an instance of Buffer or ArrayBuffer. Received an instance of Object

I added the following, minimal test case to integration-tests/tests/pg/postgres-js.test.ts

typescript
test('insert json blob', async () => {
    const jsonBlobTable = pgTable('json_insert_test', {
        id: serial('id').primaryKey(),
        content: json('content').notNull(),
    });

    await db.execute(sql`drop table if exists ${jsonBlobTable}`);

    await db.execute(sql`
      create table ${jsonBlobTable} (
      	id serial primary key,
      	content json not null
      	)
    `);

    const sampleJson1 = { foo: 'bar' };
    const sampleJson2 = { foo2: 'bar2' };

    await db.insert(jsonBlobTable).values({ content: sampleJson1 });
    await db.execute<{ id: number; recorded_at: string }>(sql`insert into ${jsonBlobTable} (content) values (${sampleJson2}) returning id`);

    const rows = await db.execute<{ id: number; content: string }>(sql`select * from ${jsonBlobTable} order by id`);

    expect(rows).toEqual([
        { id: 1, content: sampleJson1 },
        { id: 2, content: sampleJson2 },
    ]);

    await db.execute(sql`drop table if exists ${jsonBlobTable}`);
});

Expected behavior

I expect the json to be inserted correctly into the database an no error to be thrown

Environment & setup

I am using MacOS however this issue is also reproducable in GH actions/linux CI

Source: drizzle-team/drizzle-orm