Skip to content

Use $like on JSON data type #5409

Closed

Description

@pensierinmusica

Hi,

I'm using Sequelize with PSQL.

I need to build a destroy query that can use the $like operator on a JSON data type.

For example, let's take this model definition:

var Sequelize = require('sequelize');
var sequelize = new Sequelize('database', 'username', 'password');

var Project = sequelize.define('project', {
  name: Sequelize.STRING,
  details: {
    type: Sequelize.JSON,
    defaultValue: {}
  },
});

Now, I would like to be able to run a query to destroy all Project entries that don't have any details:

Project.destroy({
  where: {
    details: {
      $like: '{}'
    }
  }
})

But I get this error:

[SequelizeDatabaseError: operator does not exist: json ~~ unknown]

So, is there any way to do this and how?

Thanks!!

Activity

  1. janmeier commented on Feb 12, 2016

    @janmeier
    Member

    Can't you just use

    Project.destroy({
      where: {
        details: {}
      }
    })
  2. pensierinmusica commented on Feb 12, 2016

    @pensierinmusica
    ContributorAuthor

    @janmeier what you suggest seems to destroy all instances, and not just the ones with an empty JSON. Can you please check? Thx

  3. janmeier commented on Feb 12, 2016

    @janmeier
    Member

    Hmm, that's because details: {} gives an empty where clause - definitely a bug.

    I think we need to add a special case to handle empty objects:

    DELETE FROM projects where details::jsonb = '{}'::jsonb;

    Note that both columns need to be cast to jsonb, since json does not support equality

  4. added
    type: bug
    dialect: postgres
    on Feb 12, 2016
  5. janmeier commented on Feb 12, 2016

    @janmeier
    Member
  6. janmeier commented on Feb 12, 2016

    @janmeier
    Member

    On a side-note, this case also shows that we probably need to add a safeguard against update / delete with empty where clauses, for these cases where the where clause is interpreted incorrectly

  7. pensierinmusica commented on Feb 12, 2016

    @pensierinmusica
    ContributorAuthor

    @janmeier agree to add a safeguard against update / delete with empty where clauses.

    Regarding the rest, in this case I chose JSON vs JSONB for reasons and would prefer not having to change it. On the other hand, the SO article you mentioned states:

    There is no equality (or inequality) operator for the data type json as a whole.

    I wish PostgreSQL offered a quick, easy way to check if the JSON is empty. It seems like it currently doesn't though, right?

    My workaround in the end will probably be to keep another column as a "flag" boolean to know if the JSON is empty or not. I wish there was a more elegant solution, let me know if you find one.

    Thanks! Cheers

  8. janmeier commented on Feb 12, 2016

    @janmeier
    Member

    Should be possible to implement the empty check by casting the json to string though :-) (in sequelize, not in your own code)

  9. pensierinmusica commented on Feb 12, 2016

    @pensierinmusica
    ContributorAuthor

    @janmeier do you mean permanently changing the column data type from JSON to String, or going through each row, read the values and output them as text?

  10. janmeier commented on Feb 12, 2016

    @janmeier
    Member

    No, you don't need to change anything, I'm saying that for this particular query we need to cast the field to a string to check whether it's empty

  11. pensierinmusica commented on Feb 29, 2016

    @pensierinmusica
    ContributorAuthor

    Sure, this means though that you need to read each row in the db and can't keep an index, right?

  12. pensierinmusica commented on Jul 7, 2017

    @pensierinmusica
    ContributorAuthor

    @janmeier why has this been closed?

  13. GabeAtWork commented on May 9, 2018

    @GabeAtWork

    I'm experiencing the same issue, as I'm attempting to find a string in a JSON field. It returns the following error: operator does not exist: json ~~ unknown.

    That same query works fine on a STRING field, though. I'm puzzled, as the docs specify that the field should be treated as plain string on PostgreSQL (which I'm using): http://docs.sequelizejs.com/manual/tutorial/querying.html

    I switched to JSONB for the purposes of filtering, as an alternative, but I do believe that the current JSON behavior is a bug.

  14. oaeide commented on Sep 10, 2018

    @oaeide

    I have the same problem as @GabeAtWork

  15. added
    topic: operators
    on Oct 6, 2022
  16. ephys commented on Feb 25, 2023

    @ephys
    Member

    #15598 implements this.

    Project.destroy({
      where: {
        // Depending on the dialect you may need to cast the value like this:
        'details::text': {
          [Op.like]: '{}'
        }
      }
    })

    Produces

    DELETE FROM "Project" WHERE CAST("details" AS TEXT) LIKE '{}'
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: postgrestopic: operatorstype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions