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

SQLite: Unable to handle Take nested in Select #21078

Closed
TobiasWolters opened this issue May 29, 2020 · 4 comments · Fixed by #25835
Closed

SQLite: Unable to handle Take nested in Select #21078

TobiasWolters opened this issue May 29, 2020 · 4 comments · Fixed by #25835
Assignees
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@TobiasWolters
Copy link

Using Take nested in a select with a one-to-many relationship generates invalid SQL for SQLite.

To Reproduce

I have two classes: A and B with a one-to-many relationship. E.g.:

public class A {
     public int Id { get; set; }
     public IEnumerable<B> Bs { get; set; } 
}

public class B {
    public int Id { get; set; }
    public A TheA { get; set; }
    public int AId { get; set; }
}

The DbContext class contains the following:

public DbSet<A> As { get; set; }
public DbSet<B> Bs { get; set; }

A database table exists for each entity.

I then execute the following query:

var selectedAs = await context.As.Select(a => new A {
   Id = a.Id,
   Bs = a.Bs.Take(n)
}).ToListAsync();

, where n is an integer.

With SQLServer, everything works fine. However, when running the query in unit tests (SQLite), I get the following error:

fail: Microsoft.EntityFrameworkCore.Query[10100]
      An exception occurred while iterating over the results of a query for context type <DBContextClass>.
      Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'near "(": syntax error'.
         at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
         at Microsoft.Data.Sqlite.SqliteCommand.PrepareAndEnumerateStatements(Stopwatch timer)+MoveNext()
         at Microsoft.Data.Sqlite.SqliteCommand.GetStatements(Stopwatch timer)+MoveNext()
         at Microsoft.Data.Sqlite.SqliteDataReader.NextResult()
         at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
         at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
         at Microsoft.Data.Sqlite.SqliteCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'near "(": syntax error'.
   at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
   at Microsoft.Data.Sqlite.SqliteCommand.PrepareAndEnumerateStatements(Stopwatch timer)+MoveNext()
   at Microsoft.Data.Sqlite.SqliteCommand.GetStatements(Stopwatch timer)+MoveNext()
   at Microsoft.Data.Sqlite.SqliteDataReader.NextResult()
   at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
   at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
   at Microsoft.Data.Sqlite.SqliteCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()

Note

It manages to generate SQL, but the SQL seems to be invalid for SQLite.

Additional context

Microsoft.Data.Sqlite version: 3.1.4
Target framework: .NET Core 3.1
Operating system: Windows 10 Pro.

@maumar
Copy link
Contributor

maumar commented May 29, 2020

dupe of #19178

@smitpatel
Copy link
Member

@maumar - Looking at the query, it should get converted to JOIN rather than APPLY.

@ajcvickers ajcvickers added this to the Backlog milestone May 29, 2020
@PascalTurbo
Copy link

I got an issue that may be related to this one. The interesting thing is, that it only occurs on Windows, not on MacOS.

List<ForecastSummaryDetailsDTO> forecastSummaryDetails = await moduleContext.Database.ForecastItems
    .Where(i => i.ExpectedDate == expectedDate && i.ProductId == productId)
    .Select(i =>
        new ForecastSummaryDetailsDTO()
        {
            ForecastItemId = i.Id,
            Forecast = Database.Forecasts.First(f => f.Id == i.ForecastId)
            Quantity = i.Quantity
        }).ToListAsync();

The selection of Forecast = Database.Forecasts.First(f => f.Id == i.ForecastId) leads to the same error as @TobiasNissen reported.

@ajcvickers ajcvickers modified the milestones: Backlog, 6.0.0 Nov 5, 2020
@smitpatel
Copy link
Member

Related #24474

@maumar maumar added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 1, 2021
maumar added a commit that referenced this issue Sep 2, 2021
Resolves #13560
Resolves #17364
Resolves #17803
Resolves #21078
Resolves #21828
Resolves #23041
maumar added a commit that referenced this issue Sep 2, 2021
Resolves #13560
Resolves #17364
Resolves #17803
Resolves #21078
Resolves #21828
Resolves #23041
maumar added a commit that referenced this issue Sep 2, 2021
Resolves #13560
Resolves #17364
Resolves #17803
Resolves #21078
Resolves #21828
Resolves #23041
maumar added a commit that referenced this issue Sep 2, 2021
Resolves #13560
Resolves #17364
Resolves #17803
Resolves #21078
Resolves #21828
Resolves #23041
@maumar maumar closed this as completed in d0b9686 Sep 2, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0, 6.0.0-rc2 Sep 4, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0-rc2, 6.0.0 Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants