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

Invalid column name: orderby uses a column alias that does not exist #12180

Closed
mcorthout opened this issue May 31, 2018 · 9 comments
Closed

Invalid column name: orderby uses a column alias that does not exist #12180

mcorthout opened this issue May 31, 2018 · 9 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression type-bug
Milestone

Comments

@mcorthout
Copy link

Under rather peculiar circumstances, orderby tries to use a column alias in the ORDER BY clause that is not present in the SELECT clause.

Exception:

fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
      Failed executing DbCommand (29ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [item].[Id], [item].[Name] AS [AnotherName]
      FROM [Items] AS [item]
      ORDER BY [item].[Id], [SomeName]
System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'SomeName'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)

Steps to reproduce

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace EFCoreBugTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new Context())
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                var problem = from item in context.Items
                              orderby item.Id, item.Name
                              select new SomeOtherObject
                              {
                                  Id = item.Id,
                                  SomeName = item.Name,
                                  AnotherName = item.Name
                              };

                var boom = problem.ToList();
            }

            Console.ReadLine();

        }
    }

    public class Context : DbContext
    {
        public static readonly LoggerFactory logger = new LoggerFactory(new[] { new ConsoleLoggerProvider((_, __) => true, true) });

        public DbSet<Item> Items { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseLoggerFactory(logger);
            optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=master;Database=demo;Integrated Security=True");
        }        
    }

    public class Item
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class SomeOtherObject
    {
        public int Id { get; set; }
        public string SomeName { get; set; }
        public string AnotherName { get; set; }
    }
}

Expected result

SELECT [item].[Id], [item].[Name] AS [AnotherName]
FROM [Items] AS [item]
ORDER BY [item].[Id], [item].[Name] 

Actual result

SELECT [item].[Id], [item].[Name] AS [AnotherName]
FROM [Items] AS [item]
ORDER BY [item].[Id], [SomeName]

Further technical details

EF Core version: 2.1.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 7
IDE: Visual Studio 2017 15.7.2

@ajcvickers
Copy link
Contributor

@mcorthout Do you know if this was working with 2.0 release?

@smitpatel
Copy link
Contributor

@ajcvickers - Its a regression from 2.0.2

@ajcvickers
Copy link
Contributor

@divega Looks like a reasonably common case--should we patch?

@smitpatel Do you know if there is a workaround?

@smitpatel
Copy link
Contributor

It is slightly uncommon to include same property multiple times in the selector. (alongwith the same property being used in ordering which is necessary for bug to reproduce)
The easiest work-around would be to remove duplicated property reference. (In this case remove AnotherName = item.Name line). In case of DTO like above it may not be always possible in that case the query could be rewritten like following:

                var problem = from a in 
                              (from item in context.Items
                               orderby item.Id, item.Name
                              select new { item.Id, item.Name }
                              ).ToList()
                               select new SomeOtherObject
                              {
                                  Id = a.Id,
                                  SomeName = a.Name,
                                  AnotherName = a.Name
                              };

It cause client eval for DTO part but it will not pull any additional data from server. In case of the DTO is inside subquery it is going to cause client eval anyway so above work-around does not worsen the query.

@smitpatel smitpatel self-assigned this May 31, 2018
smitpatel added a commit that referenced this issue May 31, 2018
…s-ing

Issue:
Since the projection has same property repeated twice in the DTO, we would add alias from member name. We match projection based on unwrapped alias but we don't do that in ordering.
So we updated the projection but we did not update the ordering for new alias.
Fix:
Remember the removed projection and also use that while searching inside order by so we update ordering too and don't end up with incorrect column in order by list

Resolves #12180
smitpatel added a commit that referenced this issue May 31, 2018
…s-ing

Issue:
Since the projection has same property repeated twice in the DTO, we would add alias from member name. We match projection based on unwrapped alias but we don't do that in ordering.
So we updated the projection but we did not update the ordering for new alias.
Fix:
Remember the removed projection and also use that while searching inside order by so we update ordering too and don't end up with incorrect column in order by list

Resolves #12180
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label May 31, 2018
@smitpatel smitpatel reopened this May 31, 2018
@ajcvickers
Copy link
Contributor

Triage: after considering the fix, we think the risk is low enough that we can take this as a patch.

@ajcvickers ajcvickers added this to the 2.1.3 milestone Jun 4, 2018
@fschlaef
Copy link

fschlaef commented Jun 5, 2018

I also had this issue. The workaround works, but keep in mind that this :

context.Address.Select(a => new
{
  a.ClientFk,
  a.ClientFkNavigation.Id,
});

Will also throw the error because it is effectively the same property, even though it is not obvious at first glance.

@ajcvickers
Copy link
Contributor

This is approved for 2.1.3. Do not merge yet; branch is expected to open Monday.

Given these issues have been parked for some time, please be careful to ensure the correct commits get into the correct branches.

@ajcvickers
Copy link
Contributor

@smitpatel This issue is approved for patch and the release\2.1 branch is now open for merging. Please ensure:

  • The appropriate changes get into both the release and dev branches
  • Quirking is included for the release branch only

@smitpatel
Copy link
Contributor

Ported to release in 23e9fc8
Already merged to dev in 995822a

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. customer-reported regression type-bug
Projects
None yet
Development

No branches or pull requests

4 participants