Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jsonb column property null check result in wrong SQL condition #1674

Closed
vndlczndr opened this issue Jan 28, 2021 · 2 comments · Fixed by #1675
Closed

Jsonb column property null check result in wrong SQL condition #1674

vndlczndr opened this issue Jan 28, 2021 · 2 comments · Fixed by #1675
Assignees
Labels
bug Something isn't working
Milestone

Comments

@vndlczndr
Copy link

Hi,

I have a problem where newer versions of Npgsql for EF creates wrong SQL query conditions. See the example below.

Minimal working example:

.NET:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Npgsql;

namespace NpgSqlPoc {

    class Program {

        static void Main(string[] args) {

            NpgsqlConnection.GlobalTypeMapper.UseJsonNet();
            DatabaseContext context = new DatabaseContext();

            List<Entity> res = context.Entities.Where(x => x.JsonColumn.StringValue == null).ToList();

            Console.ReadLine();
        }
    }

    public class DatabaseContext : DbContext {
        public DbSet<Entity> Entities { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {

            optionsBuilder.UseNpgsql("Host=*****;Database=TEMP2;Username=postgres;Password=*****");
            //optionsBuilder.UseLoggerFactory(LoggerFactory.Create(x => x.AddDebug())); // 3.1.11
            //optionsBuilder.LogTo(message => Debug.WriteLine(message)); // 5.0.X
            optionsBuilder.EnableDetailedErrors();
            optionsBuilder.EnableSensitiveDataLogging();
        }
    }

    public class Entity {

        [Key]
        public int Key { get; set; }

        [Column(TypeName = "jsonb")]
        public JsonColumn JsonColumn { get; set; }
    }

    public class JsonColumn {

        public string StringValue { get; set; }
    }
}

SQL:

CREATE TABLE "public"."Entities" (
  "JsonColumn" jsonb NOT NULL,
  "Key" int4 NOT NULL DEFAULT nextval('"Entities_Key_seq"'::regclass)
)
;
ALTER TABLE "public"."Entities" OWNER TO "postgres";

-- ----------------------------
-- Primary Key structure for table Entities
-- ----------------------------
ALTER TABLE "public"."Entities" ADD CONSTRAINT "Entities_pkey" PRIMARY KEY ("Key");

Resulting SQL query using Npgsql.EntityFrameworkCore.PostgreSQL version 3.1.11:

Executed DbCommand (7ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT e."Key", e."JsonColumn"
FROM "Entities" AS e
WHERE (e."JsonColumn"->>'StringValue' IS NULL)

Resulting SQL query using Npgsql.EntityFrameworkCore.PostgreSQL version 5.0.X:

Executed DbCommand (16ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT e."Key", e."JsonColumn"
FROM "Entities" AS e
WHERE FALSE

Please note the "WHERE FALSE" condition, that results in an empty result set regardless of the real table data.

PostgreSQL version: 10.15

.NET version: 5.0

Any thoughts?

Best Regards,
Vendel

(this is my first ticket, please tell if there is a problem with it)

roji added a commit to roji/efcore.pg that referenced this issue Jan 28, 2021
@roji roji self-assigned this Jan 28, 2021
@roji roji added the bug Something isn't working label Jan 28, 2021
@roji roji added this to the 5.0.3 milestone Jan 28, 2021
@roji
Copy link
Member

roji commented Jan 28, 2021

@VendelCzinder thanks, found the issue and submitted a fix for 5.0.3. The issue is perfect, contains all the info :)

@roji
Copy link
Member

roji commented May 16, 2021

Note, this is very similar to #1683, but that one is for arrays whereas this one is for JSON.

roji added a commit to roji/efcore.pg that referenced this issue May 16, 2021
roji added a commit that referenced this issue May 16, 2021
roji added a commit that referenced this issue May 16, 2021
Fixes #1674

(cherry picked from commit b9db1f7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants