-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Invalid SQL generated with 3.1 #20505
Comments
@maumar to look for dupe. |
this is actually still broken on master. Full repro: [ConditionalFact]
public virtual void Test()
{
using (var dbContext = new MyContext())
{
dbContext.Database.EnsureDeleted();
dbContext.Database.EnsureCreated();
var t = dbContext.Floors.Where(f => f.Id == 1)
.Select(f => new
{
SpaceGroups = f.Spaces
.SelectMany(fs => fs.SpaceGroupFloorSpaces.Select(sgfs => sgfs.SpaceGroup))
.Distinct()
.OrderBy(s => s.Name)
.Select(g => new
{
g.Id,
}),
}).FirstOrDefault();
}
}
public class Floor
{
public int Id { get; set; }
public List<Space> Spaces { get; set; }
}
public class Space
{
public int Id { get; set; }
public List<SpaceGroupFloorSpace> SpaceGroupFloorSpaces { get; set; }
}
public class SpaceGroup
{
public int Id { get; set; }
public string Name { get; set; }
public List<SpaceGroupFloorSpace> SpaceGroupFloorSpaces { get; set; }
}
public class SpaceGroupFloorSpace
{
public int SpaceGroupId { get; set; }
public int SpaceId { get; set; }
public SpaceGroup SpaceGroup { get; set; }
public Space Space { get; set; }
}
public class MyContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SpaceGroupFloorSpace>()
.HasKey(x => new { x.SpaceGroupId, x.SpaceId });
modelBuilder.Entity<SpaceGroupFloorSpace>()
.HasOne(x => x.SpaceGroup)
.WithMany(x => x.SpaceGroupFloorSpaces)
.HasForeignKey(x => x.SpaceGroupId);
modelBuilder.Entity<SpaceGroupFloorSpace>()
.HasOne(x => x.Space)
.WithMany(x => x.SpaceGroupFloorSpaces)
.HasForeignKey(x => x.SpaceId);
}
public DbSet<Floor> Floors { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Repro20505;Trusted_Connection=True;MultipleActiveResultSets=true");
}
} sql: SELECT [t].[Id], [t].[Id]
FROM (
SELECT TOP(1) [f].[Id]
FROM [Floors] AS [f]
WHERE [f].[Id] = 1
) AS [t]
OUTER APPLY (
SELECT DISTINCT [t0].[Id], [t0].[Name]
FROM [Space] AS [s]
INNER JOIN (
SELECT [s1].[Id], [s1].[Name], [s0].[SpaceGroupId], [s0].[SpaceId]
FROM [SpaceGroupFloorSpace] AS [s0]
INNER JOIN [SpaceGroup] AS [s1] ON [s0].[SpaceGroupId] = [s1].[Id]
) AS [t0] ON [s].[Id] = [t0].[SpaceId]
WHERE [t].[Id] = [s].[FloorId]
) AS [t1]
ORDER BY [t].[Id], [t].[Name], [t].[Id] issues are in order by: t.Name is invalid (should be t1), t.Id specified twice in order by (should be de-duplicated and removed) |
@lukemurray @ajcvickers workaround: change @smitpatel similar to my issue #17809 ? |
forgot to ping @maumar to bring the entire team's attention on this query generation bug :) |
Duplicate of #15873 I am not sure we can correctly translate this query. With the presence of Distinct we don't have enough data to figure out when to create new groups. |
@maumar does this mean this issue will not be fixed? |
@maumar are we now tracking this on #15873 ? Any plans that this will be fixed in a 3.1.x release? @smitpatel why do you say you can't translate it? I can fix the resulting SQL without adding any new joins or aliases so there should be enough information already right? If I knew the code base I'd happily have a go fixing this, done a lot of SQL translations from .net in the past. |
@lukemurray - It cannot be translated because
Since you are only selecting one element from Floors, consider rewriting query to do a where predicate on Floor.Id rather than composition via navigation. |
Just want to confirm that using var t = dbContext.Floors.Where(f => f.Id == 1)
.Select(f => new
{
SpaceGroups = f.Spaces
.SelectMany(fs => fs.SpaceGroupFloorSpaces.Select(sgfs => sgfs.SpaceGroup))
.Distinct()
.OrderBy(s => s.Name)
.Select(g => new
{
g.Id,
}),
})
.ToList()
.FirstOrDefault(); Which leads me to believe without the |
Hi, I am upgrading a project to dotnet 3.1 and EF 3.1 and therefore Npgsql too. A query that previously worked in 2.2 generates incorrect SQL now.
Moving from npgsql/efcore.pg#1331 as suggested.
Steps to reproduce
Here is the C# query.
The relationships here:
Floor
has 0-manySpace
s, eachSpace
can have 0-manySpaceGroup
s. TheDistinct()
is there as many spaces might be assigned to the sameSpaceGroup
.Worth noting that if I remove the
.Distinct()
it works. Or if I leave the.Distinct()
but remove the.OrderBy(s => s.Name)
it also works. But both together it fails.Here is the SQL it generates. I have replaced the parameters. Note the entity also has a default filter for the current date on it.
Error is that
t.name
does not exist.Further technical details
EF Core version: 3.1
Database provider: NPGSQL
Target framework: 3.1
Operating system: Mac OS
IDE: VS Code
The text was updated successfully, but these errors were encountered: