You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PostgresException: 22007: invalid input syntax for type interval: " days" #2889
usingMicrosoft.EntityFrameworkCore;usingNpgsql;usingXunit;namespaceDateOnlyNpgsql;publicclassLinked{publicintId{get;set;}publicintDiff{get;set;}}publicclassTestEntity{publicintId{get;set;}publicDateOnly?Date{get;set;}publicintDiff{get;set;}publicLinkedLinked{get;set;}publicint?LinkedId{get;set;}}publicclassMyTest{[Fact]publicasyncTaskPostgresExceptionExample(){varx=newDbCtx();awaitx.Database.EnsureDeletedAsync();awaitx.Database.EnsureCreatedAsync();vare=newTestEntity(){Id=1,Date=DateOnly.Parse("2023-09-26")};x.TestEntities.Add(e);awaitx.SaveChangesAsync();varr1=x.TestEntities.Select(x =>new{x.Id,NextMonth=x.Date.Value.AddDays(x.Linked.Diff)});// PostgresException: 22007: invalid input syntax for type interval: " days"vara1=r1.ToArray();}}publicclassDbCtx:DbContext{publicDbSet<TestEntity>TestEntities{get;set;}=null!;publicDbSet<Linked>LinkedEntities{get;set;}=null!;protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptions){options.EnableSensitiveDataLogging();options.UseNpgsql(newNpgsqlConnection("Host=localhost;Uid=postgres;Pwd=mysecretpassword;Database=DummyDatabase"));}}
From logs I can extract this:
SELECT t."Id", t."Date"+ CAST((COALESCE(l."Diff"::text, '') ||' days') AS interval) AS"NextMonth"FROM"TestEntities"AS t
LEFT JOIN"LinkedEntities"AS l ON t."LinkedId"= l."Id"
Looks like COALESCE(l."Diff"::text, '') should be replaced with COALESCE(l."Diff"::text, '0')
I am unable to reproduce this with the latest versions (EF Core 8.0.7, Npgsql.EntityFrameworkCore.PostgreSQL 8.0.4). Using the following attempted repro:
SELECT t."Id", t."Date"+ l."Diff"AS"NextMonth"FROM"TestEntities"AS t
LEFT JOIN"LinkedEntities"AS l ON t."LinkedId"= l."Id"
The COALESCE was probably removed at some point, making this work. @dedalexij or @pil0t, if you can submit an updated repro that shows this actually failing on the latest versions, I'd be happy to take another look.
I ran into this issue in 9.0. To reproduce it, make Linked.Diff nullable and don't assign it a value. Workaround: NextMonth = x.Linked.Diff == null ? null : x.Date.Value.AddDays(x.Linked.Diff)
@jamend can you please post a console program showing this? As always, posting a partial snippet isn't sufficient - you're basically asking me to guess the context around it.
@roji In reproducing the issue I realized my use case is a bit different from the original (DateTime instead of DateOnly), so perhaps it has since been fixed for DateOnly, but I do have the issue with DateTime.
Note the main difference between the original repro and the one you posted is there is no related Linked entity, so x.Linked.Diff projects to null.
await using var context = new BlogContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
var e = new TestEntity()
{
Id = 1,
Date = DateTime.UtcNow
};
context.TestEntities.Add(e);
await context.SaveChangesAsync();
var r1 = context.TestEntities
.Select(x => new
{
x.Id,
NextMonth = x.Date.Value.AddDays(x.Linked.Diff)
});
// PostgresException: 22007: invalid input syntax for type interval: " days"
var a1 = r1.ToArray();
public class BlogContext : DbContext
{
public DbSet<TestEntity> TestEntities { get; set; } = null!;
public DbSet<Linked> LinkedEntities { get; set; } = null!;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseNpgsql("Host=localhost;Username=test;Password=test")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging()
.EnableDetailedErrors();
}
public class Linked
{
public int Id { get; set; }
public int? Diff { get; set; }
}
public class TestEntity
{
public int Id { get; set; }
public DateTime? Date { get; set; }
public int Diff { get; set; }
public Linked Linked { get; set; }
public int? LinkedId { get; set; }
}
Thanks, I can repro this (see below for a simplified repro using a single table). The issue occurs when a non-constant interval expression is generated via concatenation:
SELECT t."Id", t."Date"+ CAST(COALESCE(t."Diff"::text, '') ||' days'AS interval) AS"NextMonth"FROM"TestEntities"AS t
... and the number being concatenated is NULL.
Will reopen and use this issue to track the problem, as the original one is likely the same and did not provide a repro.
Minimal repro
awaitusingvarcontext=newBlogContext();awaitcontext.Database.EnsureDeletedAsync();awaitcontext.Database.EnsureCreatedAsync();vare=newTestEntity(){Id=1,Date=DateTime.UtcNow,Diff=null// Causes the issue};context.TestEntities.Add(e);awaitcontext.SaveChangesAsync();varr1=context.TestEntities.Select(x =>new{x.Id,NextMonth=x.Date.AddDays((double)x.Diff)}).ToArray();// PostgresException: 22007: invalid input syntax for type interval: " days"publicclassBlogContext:DbContext{publicDbSet<TestEntity>TestEntities{get;set;}protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder)=>optionsBuilder.UseNpgsql("Host=localhost;Username=test;Password=test").LogTo(Console.WriteLine,LogLevel.Information).EnableSensitiveDataLogging().EnableDetailedErrors();}publicclassTestEntity{publicintId{get;set;}publicDateTimeDate{get;set;}publicdouble?Diff{get;set;}}
The below is an AI-generated analysis and may contain inaccuracies.
Confirmed: this is still a PostgreSQL-provider bug. A single row with a null numeric column is sufficient; no relationship or optional navigation is needed. The issue is already correctly classified as Bug.
Reproduction and version history
The DateTime.AddDays repro below fails with PostgreSQL error 22007: invalid input syntax for type interval: " days" on every tested version:
Npgsql.EntityFrameworkCore.PostgreSQL
Resolved EF Core
Result
6.0.22
6.0.22
Reproduces
7.0.11
7.0.11
Reproduces
8.0.4
8.0.4
Reproduces
9.0.0
9.0.0
Reproduces
9.0.4
9.0.1
Reproduces
10.0.3
10.0.4
Reproduces
11.0.0-rc.1.1
11.0.0-rc.1.26425.128
Reproduces
These runs used PostgreSQL 18.4. This is not a regression introduced in 9.0: it also fails on 6.0.22. No known-good version was established.
The equivalent query on SQLite/EF Core 9.0.0 executes successfully and returns null. SQL Server was not tested; SQLite was used as the alternative-provider check.
minimal repro
Reference Npgsql.EntityFrameworkCore.PostgreSQL 10.0.3 in a .NET 10 console project, or 11.0.0-rc.1.1 in a .NET 11 project. Set Test__Npgsql__DefaultConnection to a disposable database, since this program deletes and recreates it.
The nullable result projection avoids conflating this server-side error with materializing a SQL null into a non-nullable DateTime.
Cause and scope
The generated SQL is:
SELECT t."Id", t."Date"+ CAST(COALESCE(t."Diff"::text, '') ||' days'AS interval) AS"NextMonth"FROM"TestEntities"AS t
The provider's CreateIntervalExpression builds non-constant intervals using string concatenation. EF's nullability processing adds COALESCE(..., '') to preserve C# string-concatenation semantics. With a null number, this produces ' days', which PostgreSQL cannot parse as an interval. Without that coalesce, PostgreSQL propagates null instead.
On provider 10.0.3, I also verified that:
DateOnly.AddDays with a null numeric column succeeds and returns null: it translates to date + integer, without interval construction.
DateOnly.AddYears with a null numeric column still fails with 22007, this time for " years".
This explains why the earlier DateOnly.AddDays test did not demonstrate the remaining interval-construction problem.
Workaround
An explicit null guard works on provider 10.0.3 and returns null:
Replacing the empty string with '0' would avoid the parse error but change null propagation into adding zero days, so it is not an equivalent fix.
Related upstream issues
dotnet/efcore#36210 is the directly related upstream investigation already linked here. It was closed as expected behavior, not fixed: C# string concatenation treats null as an empty string. This issue therefore still needs a provider-side solution rather than waiting for that upstream issue.
dotnet/efcore#3836 is the historical issue behind that string-concatenation null compensation, not a separate unresolved instance of this PostgreSQL interval bug.
No additional likely upstream duplicate was found in the targeted search. Keep this issue open as the provider-side tracking bug.
Activity
dedalexij commentedon Aug 5, 2024
I have the same problem with the AddYears method
roji commentedon Aug 5, 2024
Sorry for not responding to this earlier.
I am unable to reproduce this with the latest versions (EF Core 8.0.7, Npgsql.EntityFrameworkCore.PostgreSQL 8.0.4). Using the following attempted repro:
I can see the following SQL:
The COALESCE was probably removed at some point, making this work. @dedalexij or @pil0t, if you can submit an updated repro that shows this actually failing on the latest versions, I'd be happy to take another look.
roji commentedon Oct 28, 2024
Closing as the problem isn't reproducible at this point.
jamend commentedon Jun 3, 2025
I ran into this issue in 9.0. To reproduce it, make
Linked.Diffnullable and don't assign it a value. Workaround:NextMonth = x.Linked.Diff == null ? null : x.Date.Value.AddDays(x.Linked.Diff)roji commentedon Jun 5, 2025
@jamend can you please post a console program showing this? As always, posting a partial snippet isn't sufficient - you're basically asking me to guess the context around it.
jamend commentedon Jun 5, 2025
@roji In reproducing the issue I realized my use case is a bit different from the original (
DateTimeinstead ofDateOnly), so perhaps it has since been fixed forDateOnly, but I do have the issue withDateTime.Note the main difference between the original repro and the one you posted is there is no related
Linkedentity, sox.Linked.Diffprojects to null.roji commentedon Jun 5, 2025
Thanks, I can repro this (see below for a simplified repro using a single table). The issue occurs when a non-constant interval expression is generated via concatenation:
... and the number being concatenated is NULL.
Will reopen and use this issue to track the problem, as the original one is likely the same and did not provide a repro.
Minimal repro
jamend commentedon Jun 5, 2025
I was also able to reproduce it with
DateOnlywith a test case inGearsOfWarQueryNpgsqlTest:roji commentedon Jun 8, 2025
Root cause: dotnet/efcore#36210
roji commentedon Oct 18, 2026
AI Triage
The below is an AI-generated analysis and may contain inaccuracies.
Confirmed: this is still a PostgreSQL-provider bug. A single row with a null numeric column is sufficient; no relationship or optional navigation is needed. The issue is already correctly classified as Bug.
Reproduction and version history
The
DateTime.AddDaysrepro below fails with PostgreSQL error22007: invalid input syntax for type interval: " days"on every tested version:These runs used PostgreSQL 18.4. This is not a regression introduced in 9.0: it also fails on 6.0.22. No known-good version was established.
The equivalent query on SQLite/EF Core 9.0.0 executes successfully and returns null. SQL Server was not tested; SQLite was used as the alternative-provider check.
minimal repro
Reference
Npgsql.EntityFrameworkCore.PostgreSQL10.0.3 in a .NET 10 console project, or 11.0.0-rc.1.1 in a .NET 11 project. SetTest__Npgsql__DefaultConnectionto a disposable database, since this program deletes and recreates it.The nullable result projection avoids conflating this server-side error with materializing a SQL null into a non-nullable
DateTime.Cause and scope
The generated SQL is:
The provider's CreateIntervalExpression builds non-constant intervals using string concatenation. EF's nullability processing adds
COALESCE(..., '')to preserve C# string-concatenation semantics. With a null number, this produces' days', which PostgreSQL cannot parse as an interval. Without that coalesce, PostgreSQL propagates null instead.On provider 10.0.3, I also verified that:
DateOnly.AddDayswith a null numeric column succeeds and returns null: it translates todate + integer, without interval construction.DateOnly.AddYearswith a null numeric column still fails with22007, this time for" years".This explains why the earlier
DateOnly.AddDaystest did not demonstrate the remaining interval-construction problem.Workaround
An explicit null guard works on provider 10.0.3 and returns null:
Replacing the empty string with
'0'would avoid the parse error but change null propagation into adding zero days, so it is not an equivalent fix.Related upstream issues
No additional likely upstream duplicate was found in the targeted search. Keep this issue open as the provider-side tracking bug.