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

Table alias collision in generated SQL with subquery (MSSQL) #3826

Closed
jparish9 opened this issue Nov 20, 2015 · 1 comment
Closed

Table alias collision in generated SQL with subquery (MSSQL) #3826

jparish9 opened this issue Nov 20, 2015 · 1 comment
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@jparish9
Copy link

Windows 10, VS 2015, IIS Express, SQL Server Express 2014, EF 7.0.0-rc1-final, ASP.NET 6.0.0-rc1-final

I am seeing incorrect SQL generated with subqueries when the tables involved begin with the same letter.

Models:

public class Blog
{
        public int BlogId { get; set; }

        public string Title { get; set; }

        public ICollection<BlogPost> BlogPosts { get; set; }
}
public class BlogPost
{
        public int BlogPostId { get; set; }

        public int BlogId { get; set; }

        public Blog Blog { get; set; }

        public string PostText { get; set; }
}

FK setup:

    modelBuilder.Entity<Blog>().HasMany(b => b.BlogPosts).WithOne(p => p.Blog);

Query:

   _dbContext.Blogs.Where(b => b.BlogPosts.Count == 0).ToList();

Generated SQL:

SELECT [b].[BlogId], [b].[Title]
FROM [Blog] AS [b]
WHERE (
    SELECT COUNT(*)
    FROM [BlogPost] AS [b]
    WHERE [b].[BlogId] = [b].[BlogId]
) = 0

With test data of 2 blogs, 2 posts for blog 1 and 0 for blog 2, this query incorrectly returns 0 results (should return one result, namely blog 2). If BlogPost in the subquery is aliased to something other than [b] it works fine, e.g.:

SELECT [b].[BlogId], [b].[Title]
FROM [Blog] AS [b]
WHERE (
    SELECT COUNT(*)
    FROM [BlogPost] AS [bp]
    WHERE [bp].[BlogId] = [b].[BlogId]
) = 0
@rowanmiller rowanmiller added this to the 7.0.0 milestone Nov 24, 2015
@rowanmiller rowanmiller modified the milestones: 7.0.0-rc2, 7.0.0 Dec 2, 2015
@rowanmiller rowanmiller assigned smitpatel and unassigned maumar Dec 2, 2015
@smitpatel
Copy link
Contributor

The issue here is,
For generated subquery, when we create table alias, we use first character of the entity in lower case. If that clashes with user defined alias then error happens. Simple workaround for now would be to use different character like this:
_dbContext.Blogs.Where(p => p.BlogPosts.Count == 0).ToList();

@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
@ajcvickers ajcvickers modified the milestones: 1.0.0-rc2, 1.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

5 participants