Skip to content

Wrong alias in Where clause if using Fetch and scalar Select #3263

@filronius

Description

@filronius

I have two entities (Employee and OptionaInfo) which are linked over HasOne mappings. When running this query:

 _session.Query<Employee>()
    .Fetch(x => x.OptionalInfo)
    .Where(x => x.OptionalInfo != null)
    .Select(x => x.OptionalInfo.Age)
    .ToList();

I get this error:

could not execute query
[
    select optionalin1_.Age as col_0_0_ 
    from Employee employee0_
    left outer join OptionalInfo optionalin1_ on employee0_.EmployeeId=optionalin1_.EmployeeId
    where optionalin2_.EmployeeId is not null
]
PostgresException: 42P01: missing FROM-clause entry for table »optionalin2_«

NHibernate uses alias optionalin2_ but doesn’t define it. If I change optionalin2_ to optionalin1_ manually, the query works.
The error only occurs if the Fetch method is present. Without Fetch everything works as expected.
I tested NHibernate versions 5.4.1, 5.4.0 and 5.3.15. The error appears in 5.4.1 and 5.4.0. Version 5.3.15 works just fine. I'm using PostgreSQL.

Here are my entities and maps:

public class Employee
{
    public virtual int EmployeeId { get; set; }
    public virtual string Name { get; set; }
    public virtual OptionalInfo? OptionalInfo { get; set; }
}

public class OptionalInfo
{
    public virtual int EmployeeId { get; set; }
    public virtual int Age { get; set; }
    public virtual Employee Employee { get; set; }
}

public class EmployeeMap : ClassMap<Employee>
{
    public EmployeeMap()
    {
       Table("Employee");
        Id(x => x.EmployeeId).GeneratedBy.Identity();
        Map(x => x.Name).Not.Nullable();
        HasOne(x => x.OptionalInfo).Cascade.SaveUpdate();
    }
}

public class OptionalInfoMap : ClassMap<OptionalInfo>
{
    public OptionalInfoMap()
    {
        Table("OptionalInfo");
        Id(x => x.EmployeeId).GeneratedBy.Foreign("Employee");
        Map(x => x.Age).Not.Nullable();
        HasOne(x => x.Employee).Cascade.None();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions