question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No translation for dictionary indexer on jsonb columns

See original GitHub issue

Consider the following entity:

public class User
{
      [Key]
      public Guid Id { get; set; }

      [Column(TypeName = "jsonb")]
      public Dictionary<string, object> Properties { get; set; }
}

And then querying:

context.User.Where(x => x.Properties["something"] == "something else").ToList();

Ends up not being able to translate.

Translating the method call of get_Item to something npgsql can understand has proven to be difficult. And one would assume translating it into the proper binding is not possible due to the dynamic nature. I would assume some internal work is needed to properly translate get_Item into sql directly instead.

I looked into perhaps doing a custom Visitor to handle this case but my Expression knowledge lacks in this case and it does not seem possible in the end anyways. Also to clarify, changing the dictionary to a JsonDocument type is not warranted in this case.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:7
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rojicommented, Oct 10, 2020

Thanks for the added details. Yeah, mapping Dictionary<string, object> and translating the indexer seems like a very valid scenario to me - I’ve noted it for 6.0 (I’ll probably be doing work on this in EF itself). Jsonpath is definitely also on my list, tracked by https://github.com/npgsql/efcore.pg/issues/1045 for PG specifically - but again I’ll look into this from a cross-database perspective for 6.0.

Will keep this open for now to specifically track dictionary indexer for EFCore.PG.

1reaction
rojicommented, Oct 10, 2020

This is indeed unsupported at the moment. The mapping itself works since System.Text.Json can serialize/deserialize dictionary as JSON, but the query pipeline does not support the translating of the dictionary indexer, as you’ve noticed. There’s nothing impossible or special about it - just like we translate the various methods on JsonDocument, it’s possible to translate this.

We are planning a general JSON overhaul for EF Core 6.0 - not just for the PostgreSQL provider - so at this point I’m a bit reluctant to make this change. Is there any specific reason you don’t want to use JsonDocument, except for aesthetics?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Indexing of tuples in a large PSQL JSONB array
I have a JSONB column "deps" like this ... Is it possible to create an index of the items by "name" field of...
Read more >
JSONB PostgreSQL: How To Store & Index JSON Data
In this post, we are going to show you tips and techniques on how to effectively store and index JSON data in PostgreSQL....
Read more >
Postgresql index for jsonb array field
One problem with document types like jsonb : Postgres currently does not maintain statistics about value frequencies of embedded elements (still ...
Read more >
JSON Mapping | Npgsql Documentation
The Npgsql EF Core provider allows you to map PostgreSQL JSON columns in three different ways: As simple strings; As strongly-typed user-defined types...
Read more >
PostgreSQL and JSON – How to Use JSON Data in ...
Here's an example of creating a Table Journal and giving the column “diary_information” the data type JSONB. CREATE TABLE journal ( id Int...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found