-
Notifications
You must be signed in to change notification settings - Fork 225
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
Translation skipped for array accessed by relationship #651
Comments
This is actually unrelated to owned entities and can be reproduced with a regular one-to-one relationship as shown below. Seems like when an array property is accessed by traversing a relationship, for some reason we fail to translate to SQL. @austindrenski I think you worked in these waters before, interested in taking a look? I won't have time to dive into this in the coming weeks... class Program
{
static void Main(string[] args)
{
using (var ctx = new BlogContext())
{
ctx.Database.EnsureDeleted();
ctx.Database.EnsureCreated();
// Produces good query:
// SELECT s."Id", s."Array"
// FROM "Posts" AS s
// WHERE 1 = ANY (s."Array") = TRUE
var posts1 = ctx.Posts
.Where(s => s.Array.Contains(1))
.ToList();
// Produces bad query:
// SELECT s."Id", s."PostId", "s.Post"."Array"
// FROM "Blogs" AS s
// LEFT JOIN "Posts" AS "s.Post" ON s."PostId" = "s.Post"."Id"
var posts2 = ctx.Blogs
.Where(s => s.Post.Array.Contains(1))
.ToList();
}
}
}
public class Blog
{
public int Id { get; set; }
public Post Post { get; set; }
}
public class Post
{
public int Id { get; set; }
public int[] Array { get; set; }
}
public class BlogContext : DbContext
{
public const string ConnectionString = "Host=localhost;Database=test;Username=npgsql_tests;Password=npgsql_tests";
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder builder)
=> builder.UseNpgsql(ConnectionString);
} |
Moving this to the backlog, @austindrenski you may be interested in this. |
Yes, still interested... I need to wrap up a few more issues for 2.2 first, then I'll give this a look. It may also be worth waiting for #541 to merge, since that has an effect on how array translations evaluate. |
I can reproduce this with the test suite for I cannot reproduce this with the test suite for
Tests[Fact]
public void Contains_with_navigation_property()
{
using (var ctx = Fixture.CreateContext())
{
var _ = ctx.SomeEntities.Where(e => e.SomeRelatedEntity.SomeArray.Contains(3)).ToList();
AssertContainsInSql("WHERE 3 = ANY (\"e.SomeRelatedEntity\".\"SomeArray\") = TRUE");
}
} public class SomeArrayEntity
{
public int Id { get; set; }
public int[] SomeArray { get; set; }
public int[,] SomeMatrix { get; set; }
public List<int> SomeList { get; set; }
public byte[] SomeBytea { get; set; }
public string SomeText { get; set; }
public SomeRelatedEntity SomeRelatedEntity { get; set; }
}
public class SomeRelatedEntity
{
public int Id { get; set; }
public int[] SomeArray { get; set; }
} -- full query output (formatted for clarity):
SELECT
e."Id",
e."SomeArray",
e."SomeBytea",
e."SomeList",
e."SomeMatrix",
e."SomeRelatedEntityId",
e."SomeText"
FROM "SomeEntities" AS e
LEFT JOIN "SomeRelatedEntities" AS "e.SomeRelatedEntity"
ON e."SomeRelatedEntityId" = "e.SomeRelatedEntity"."Id"
WHERE 3 = ANY ("e.SomeRelatedEntity"."SomeArray") = TRUE |
Debugging in
We could simply unwrap This does pass the test suite, but just feels a little too risky for a patch release: var subQueryModel = expression.QueryModel;
var fromExpression = subQueryModel.MainFromClause.FromExpression;
// unwrap and move on
if (fromExpression is NullConditionalExpression n)
fromExpression = n.AccessOperation; @roji Any thoughts on this? |
@austindrenski thanks for deep-diving into this and for the good analysis! I agree with everything you're saying... If the bug no longer exists in 2.2 (which is about to be released), I don't see any reason to introduce a risky fix in a 2.1 patch release... Our answer here should probably be to just upgrade to 2.2. From my side feel free to close this issue, unless you have other thoughts... |
Sounds good to me, closing for now. |
While operation translations for simple types on owned entities are working well, array operations aren't getting translated into sql query.
Configuration:
LINQ:
Actual sql query:
Expected sql query:
Actual for: Npgsql.EntityFrameworkCore.PostgreSQL v2.2.0-preview1
The text was updated successfully, but these errors were encountered: