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]: cannot insert jsonb in PGlite #3997

Open
1 task done
jamesarosen opened this issue Jan 24, 2025 · 1 comment
Open
1 task done

[BUG]: cannot insert jsonb in PGlite #3997

jamesarosen opened this issue Jan 24, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@jamesarosen
Copy link

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

0.38.2

What version of drizzle-kit are you using?

0.38.2

Other packages

@electric-sql/[email protected],[email protected]

Describe the Bug

Given a schema:

export const events = sm.table('events', {
  metadata: jsonb('metadata').default({}).notNull(),
});

I can insert rows with jsonb:

await db.insert(events).values([
  { metadata: {} },
  { metadata: { foo: 'foo 1' } },
  { metadata: { foo: 'foo 2' } },
]);

And I can retrieve rows with jsonb:

const events = await db.select().from(events);
// [ { metadata: {} }, { metadata: { "foo": "foo 1" } }, { metadata: { "foo": "foo 2" } } ]

But I can't use the ->> operator to query:

const events = await db.select().from(events).where(sql`metadata->>'foo' = ${'foo 1'}`);
// []

I verified that PGlite itself handles this fine:

const db = new PGlite(...);
await db.exec(`
  create table events(metadata jsonb);
  insert into events(metadata) values
    ('{}'::jsonb)
  , ('{ "foo": "foo 1" }'::jsonb)
  , ('{ "foo": "foo 2" }'::jsonb)
`)
await db.select(`select * from events where metadata->>'foo' is not null`)
// { rows: [ { metadata: '{"foo":"foo 1"}', } { metadata: '{"foo":"foo 2"}', } ], ... }
@jamesarosen jamesarosen added the bug Something isn't working label Jan 24, 2025
@jamesarosen
Copy link
Author

jamesarosen commented Jan 28, 2025

This was caused by #724

It seems to still be happening when using PGlite on [email protected]

See electric-sql/pglite#315

If I set debug: 1 on the PGlite instance, I get the following logs:

await db.insert(events).values({
  metadata: { via: 'js-object' },
});
/*
runQuery insert into "test"."events" ("metadata") values ($1) [ '{"via":"js-object"}' ] {
  rowMode: 'object',
  parsers: {
    '1082': [Function: 1082],
    '1114': [Function: 1114],
    '1184': [Function: 1184],
    '1186': [Function: 1186]
  }
}
*/

await db.insert(events).values({
  metadata: sql`${{ via: 'sql-jsonb' }}::jsonb`,
});
/*
runQuery insert into "test"."events" ("metadata") values ($1) [ { "via": "sql-jsonb" } ] {
  rowMode: 'object',
  parsers: {
    '1082': [Function: 1082],
    '1114': [Function: 1114],
    '1184': [Function: 1184],
    '1186': [Function: 1186]
  }
}
*/

@jamesarosen jamesarosen reopened this Jan 28, 2025
@jamesarosen jamesarosen changed the title [BUG]: cannot use ->> jsonb operator with node-postgres, PGlite [BUG]: cannot insert jsonb in PGlite Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant