Skip to content

Postgres: JSONB sub columns can't make as JSONB  #10599

Closed

Description

@haracejacob

What's the problem?

Hello, I have ARRAY type sub field in JSONB field. And I want to querying $contains to ARRAY field. but, sequelize makes nested key to TEXT

Data format (db)

{
  foo: {
    bar: ['zoo']
  }
}

Code

db.findAll(
  where: {
    foo: {
      bar: {
        [Sequelize.Op.contains]: '["zoo"]'
      }
    }
  }
)

Query

SELECT *
FROM db AS "DB"
WHERE ("DB"."foo" #>> '{bar}') @> '["zoo"]'

And occur SequelizeDatabaseError: operator does not exist: text @> unknown

What are you doing?

I found CAST Option and did it.

Code

db.findAll(
  where: {
    foo: {
      'bar::jsonb': {
        [Sequelize.Op.contains]: '["zoo"]'
      }
    }
  }
)

Query

SELECT *
FROM db AS "DB"
WHERE CAST(("DB"."foo" #>> '{bar}') AS JSONB) @> '["zoo"]'

Problem was not occured. but, this query can't use index(except function-based index)

What do you expect to happen?

So, I want to replace #>> with #> when use cast statement ::jsonb.
I fixed dialects/abstract/query-generator.js commit

And retry.

Code

db.findAll(
  where: {
    foo: {
      'bar::jsonb': {
        [Sequelize.Op.contains]: '["zoo"]'
      }
    }
  }
)

Query

SELECT *
FROM db AS "DB"
WHERE ("DB"."foo" #> '{bar}') @> '["zoo"]'

It's best result to me.

Could you please make options for choose '#>' or '#>>' or '->' or '->>' for JSONB.
Or Do I create PR? if these logic are fine.

Dialect: postgres
Dialect version: 7.8.1
Database version: 11.1
Sequelize version: 5.1

Activity

  1. stale commented on Jun 20, 2019

    @stale
  2. reopened this on Jan 15, 2020
  3. papb commented on Jan 15, 2020

    @papb
    Member

    Related: #11671

  4. papb commented on Jan 15, 2020

    @papb
    Member

    Actually, do you agree this a duplicate of #11671? Or is there something different?

  5. haracejacob commented on Jan 15, 2020

    @haracejacob
    Author

    @papb
    This issue is similar to #11671 but not duplicated.

    My opinion is(regardless of the above issue) that when accessing JSONB object fields using nested key in sequelize, It needs to branch the operator to return TEXT type(->> or #>>) or JSONB type(-> or #>) depending on the additional operator.

    case(jsonb)

    db.findAll(
      where: {
        foo: {
          bar: {
            [Sequelize.Op.contains]: '["zoo"]'
          }
        }
      }
    )

    In this case, contains(@>) operator needs jsonb type as left operand type
    so, query should be

    SELECT *
    FROM db AS "DB"
    WHERE "DB"."foo" #> '{bar}' @> '["zoo"]'

    case2(text)

    db.findAll(
      where: {
        foo: {
          bar: {
            [Sequelize.Op.like]: 'z%'
          }
        }
      }
    )

    In this case, like operator needs text type as left operand type
    so, query should be

    SELECT *
    FROM db AS "DB"
    WHERE "DB"."foo" #>> '{bar}' like 'z%'

    case3(uncertain)

    db.findAll(
      where: {
        foo: {
          bar: 'zoo'
        }
      }
    )

    Equal is uncertain. because both JSONB type and TEXT type can use equal(=) operator.
    More discussion is needed in this case.

    To use sequelize more effectively with PostgreSQL, it need the above features.
    And could you please make options for choose ('#>', '#>>') or ('->', '->>') for accessing JSONB object fields using nested key. Indexes created using one operator do not interoperate with indexes created using the other operator.

  6. papb commented on Jan 16, 2020

    @papb
    Member

    Thanks!

    Or Do I create PR?

    Please open a PR, that would be great!

  7. added
    dialect: postgres
    status: awaiting response
    type: feature
    on Jan 16, 2020
  8. ephys commented on Feb 25, 2023

    @ephys
    Member

    #15598 implements this. The JSON extraction syntax now produces JSON instead of text, so this should just work:

    User.findAll(
      where: {
        foo: {
          bar: {
            [Op.contains]: ['zoo'],
          },
        },
      },
    );

    You can still use the previous text value by writing this:

    User.findAll(
      where: {
        foo: {
          // :unquote is a modifier that "unquotes" JSON.
          // It's equivalent to json_unquote in most dialects, 
          // or the #>> and ->> operators in postgres)
          // The resulting value is TEXT, not JSON
          'bar:unquote': {
            [Op.like]: 'z%'
          }
        }
      }
    )
  9. removed
    status: awaiting response
    on Feb 25, 2023
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

    dialect: postgrestype: feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions