Skip to main content
  1. About
  2. For Teams
Asked
Viewed 67 times
1

I am trying to write an update query that updates any users that have a dog named "Mark", the table has a column named "additional_options" and it is a Json blob that looks like this

{
'dog_name': 'Mark'
}

Right now I have

update "Employees" e set country = "BR" where json_extract_path(e.additional_options, "dog_name") = "Mark"

I'm using this postgres functions found here to build this, but right now I'm getting an error

column "dog_name" does not exist

can anyone help me figure out why this isn't working?

Edit: If it helps, I wrote this query using Prisma and it works


export const moveToBrazil = async(): Promise<Employee[]> => {
    await db.employees.updateMany({
        where: {
            additional_options: {
                path: ['dog_name'],
                equals: 'Ellie'
            }
        },
        data: {
            country: "BR"
        }
    });
    return db.employees.findMany({
        where: {
            additional_options: {
                path: ['dog_name'],
                equals: 'Ellie'
            }
        },
    });
}
2
  • 1
    Postgres uses double quotes to escape reserved words or column names with spaces or such
    nbk
    –  nbk
    2023-11-04 22:12:50 +00:00
    Commented Nov 4, 2023 at 22:12
  • Thats right, I have a new problem then, it doesn't know the type of 'dog_name'. ``` json_extract_path(jsonb, unknown) does not exist ```
    Dean Packard
    –  Dean Packard
    2023-11-04 22:20:24 +00:00
    Commented Nov 4, 2023 at 22:20

1 Answer 1

1

You can make use of ->> operator to get the json object field as text. Something like:

UPDATE "Employees" e SET country = 'BR' WHERE e.additional_options->>'dog_name' = 'Mark';

The ->> operator gets the JSON object field as text. If additional_options is a jsonb column and dog_name is a key in that JSON, additional_options->>'dog_name' gets the value of dog_name as text.

CODE DEMO

REFERENCE

Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct answer! Thank you for your help and your explanation, you're the best!

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.