Skip to content

Mass Update JSONB Error #872

Closed
Closed

Description

@henryzhang03

So I am trying to do a query similar to this:

      await db`
        update example
        set
          example_object = v.example_object::jsonb,
        from (values ${db(valuesArray)}) as v (
          example_object
        )
        where example.identifier = v.identifier;`;

example_object in this case is an object in my typescript code. However, if I try to insert it this way, it complains that it is an [object Object] and can't parse it. On the other hand, if I don't include the jsonb cast, it complains that it just text. Can someone please help me resolve this issue? As it is very mission critical.

Activity

  1. henryzhang03 commented on Dec 25, 2024

    @henryzhang03
    Author

    So I am trying to do a query similar to this:

          await db`
            update example
            set
              example_object = v.example_object::jsonb,
            from (values ${db(valuesArray)}) as v (
              example_object
            )
            where example.identifier = v.identifier;`;

    example_object in this case is an object in my typescript code. However, if I try to insert it this way, it complains that it is an [object Object] and can't parse it. On the other hand, if I don't include the jsonb cast, it complains that it just text. Can someone please help me resolve this issue? As it is very mission critical.

    hello? Bumping this

  2. robahtou commented on Feb 2, 2025

    @robahtou

    Ran into this issue recently as well. Take into consideration how Postgres handles object serialization. Passing an object gets converted to a string representation [object Object]. You need to explicitly serialize the object to JSON before passing it to the query:

    await db`
      update example
      set
        example_object = v.example_object::jsonb
      from (values ${db(valuesArray.map(v => ({ ...v, example_object: JSON.stringify(v.example_object) })))}) as v (
        example_object
      )
      where example.identifier = v.identifier;
    `;
    
  3. added a commit that references this issue on Oct 29, 2026
    06cf284
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions