You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello,
I've encountered a weird behavior while using the ltree extension, in particular the Ltree.MatchesLQuery method.
Given the following code
using Microsoft.EntityFrameworkCore;usingvarcontext=new DatabaseContext();vardata= context.Categories.Where(c => c.Path.MatchesLQuery("*."+ c.Id));
Console.WriteLine(data.Count());//PostgresException (0x80004005): 42846: cannot cast type integer to lqueryclassDatabaseContext:DbContext{publicDbSet<Category> Categories {get;set;}protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder){
optionsBuilder.UseNpgsql("connection-string-here");}protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){base.OnModelCreating(modelBuilder);
modelBuilder.HasPostgresExtension("ltree");}}classCategory{publicintId{get;set;}publicstringName{get;set;}publicLTreePath{get;set;}}
the following SQL is generated
SELECT c."Id", c."Name", c."Path"FROM"Categories"AS c
WHERE c."Path" ~ ('*.'|| c."Id"::lquery)
which causes 'Unhandled exception. Npgsql.PostgresException (0x80004005): 42846: cannot cast type integer to lquery' when executed, because only the right side of the concatenation expression inside the MatchesLQuery method call is being cast into lquery.
I would expect the entire concatenation expression inside the MatchesLQuery method call (the string '*.' concatenated with the Id column) to be cast into lquery, not just the right side, producing the following SQL which would work
SELECT c."Id", c."Name", c."Path"FROM"Categories"AS c
WHERE c."Path" ~ ('*.'|| c."Id")::lquery
This only happens when concatenating with a column from the database, not a constant or a parameter. Order also does not matter. If I were to swap the left and right side or put the Id in the middle like this
it would still try to cast the Id to an lquery first and then do the concatenation like this
SELECT c."Id", c."Name", c."Path"FROM"Categories"AS c
WHERE c."Path" ~ (c."Id"::lquery ||'.*');
SELECT c."Id", c."Name", c."Path"FROM"Categories"AS c
WHERE c."Path" ~ (('*.'|| c."Id"::lquery) ||'.*');
which of course does not work.
Tested on .NET 6 and 7
Npgsql.EntityFrameworkCore.PostgreSQL 6.0.6 and 7.0.0-preview.7
Npgsql 6.0.6 and 7.0.0-preview.7
Microsoft.EntityFrameworkCore 6.0.8 and 7.0.0-preview.7.22376.2
The text was updated successfully, but these errors were encountered:
Hello,
I've encountered a weird behavior while using the ltree extension, in particular the Ltree.MatchesLQuery method.
Given the following code
the following SQL is generated
which causes 'Unhandled exception. Npgsql.PostgresException (0x80004005): 42846: cannot cast type integer to lquery' when executed, because only the right side of the concatenation expression inside the MatchesLQuery method call is being cast into lquery.
I would expect the entire concatenation expression inside the MatchesLQuery method call (the string '*.' concatenated with the Id column) to be cast into lquery, not just the right side, producing the following SQL which would work
This only happens when concatenating with a column from the database, not a constant or a parameter. Order also does not matter. If I were to swap the left and right side or put the Id in the middle like this
it would still try to cast the Id to an lquery first and then do the concatenation like this
which of course does not work.
Tested on .NET 6 and 7
Npgsql.EntityFrameworkCore.PostgreSQL 6.0.6 and 7.0.0-preview.7
Npgsql 6.0.6 and 7.0.0-preview.7
Microsoft.EntityFrameworkCore 6.0.8 and 7.0.0-preview.7.22376.2
The text was updated successfully, but these errors were encountered: