SQL error when selecting a column of a subclass when sibling classes have a column of the same name

Description

I have an entity class "DataRecord" which has four joined sub-classes "Incident", "Problem", "RequestForChange" and "Change". Each sub-class has property called "State" which is a many-to-one reference to four other tables - "IncidentState", "ProblemState", "RequestForChangeState" and "ChangeState" - respectively.

If I use the NHibernate LINQ provider to select one of "State" columns by explicitly providing the sub-class to look for, invalid SQL is created referencing the wrong sub-table.

My mapping is basically:

My LINQ query is:

The NhQueryable must use the parent class here, because the query is dynamically built and may or may not include properties of the other sub-classes.

The constructed expression, i.e. query.Expression.DebugView, is:

When executing I get the following GenericADOException:

with the InnerException:

Invalid column name 'StateDataChangeStateId'.

As you can see the last join to access ((Change)x).State is correctly using the foreign key column name "StateDataChangeStateId" of "Change" but incorrectly using the table for "Incident" (datarecord0_1_).

Instead of

it should be

.

Environment

SQLServer 2012

Attachments

1

Activity

Show:

Frédéric Delaporte 
April 1, 2017 at 9:56 AM

Binaries removed from supplied test zip, nuget dependencies added, migrated to latest release.

Frédéric Delaporte 
March 30, 2017 at 4:38 PM

Yes.

Maik Schott 
March 30, 2017 at 3:39 PM

Thank you for reopening. I will try to provide a test case. In case of the zip file I suppose I would need to attach it here?

Frédéric Delaporte 
March 30, 2017 at 3:25 PM

Reducing priority: as stated by reporter, renaming would be a workaround.

Frédéric Delaporte 
March 30, 2017 at 3:21 PM
(edited)

Ok, my last query is still semantically more correct, but there would be a bug as you state. May you please provide a test case? (Preferably through a PR on https://github.com/nhibernate/nhibernate-core after reading contributing guidelines to be found in root source folder, otherwise in a zip file by following those instructions.)

It has some similitudes with NH-2491, the code implied in this other bug could be implied here, if the bug is confirmed.

Fixed

Details

Assignee

Reporter

Labels

Components

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created March 29, 2017 at 11:58 AM
Updated October 18, 2022 at 11:58 AM
Resolved October 18, 2022 at 11:58 AM
Who's Looking?