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

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

Closed
filronius opened this issue Mar 22, 2023 · 5 comments
Closed

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

filronius opened this issue Mar 22, 2023 · 5 comments

Comments

@filronius
Copy link

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();
    }
}
@bahusoid
Copy link
Member

The error only occurs if the Fetch method is present

Might be regression from #2496

@bahusoid
Copy link
Member

bahusoid commented Mar 22, 2023

But why Fetch is present in query if you Select some scalar values? While it's a valid issue your Fetch makes no sense in this query.

@bahusoid bahusoid changed the title Wrong alias in WHERE clause if using Fetch Wrong alias in WHERE clause if using Fetch and scalar Select Mar 22, 2023
@filronius
Copy link
Author

Yes, the Fetch makes no sense in this case.

The project, in which I noticed this bug, is more complex than the example above and has included Fetch already. Unfortunately I can’t remove it there.

bahusoid added a commit that referenced this issue Mar 24, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Fixes #3263
@bahusoid
Copy link
Member

Fixed by #3267

@filronius
Copy link
Author

Thank you!

@fredericDelaporte fredericDelaporte changed the title Wrong alias in WHERE clause if using Fetch and scalar Select Wrong alias in Where clause if using Fetch and scalar Select Apr 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants