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]: inserting json does not work when using sql template string #3171

Open
Tracked by #3649
camc314 opened this issue Oct 21, 2024 · 1 comment
Open
Tracked by #3649

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

camc314 opened this issue Oct 21, 2024 · 1 comment
Assignees
Labels
bug Something isn't working db/postgres driver/postgresjs priority Will be worked on next qb/crud

Comments

@camc314
Copy link

camc314 commented Oct 21, 2024

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

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

@camc314 camc314 added the bug Something isn't working label Oct 21, 2024
@camc314 camc314 changed the title [BUG]: [BUG]: inserting json does not work when using sql string literal Oct 21, 2024
@camc314 camc314 changed the title [BUG]: inserting json does not work when using sql string literal [BUG]: inserting json does not work when using sql template string Oct 21, 2024
@L-Mario564 L-Mario564 self-assigned this Nov 29, 2024
@wojtek-fintek
Copy link

Noticed the same today after bumping drizzle-orm to newest version

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

No branches or pull requests

3 participants