NHibernate generating invalid query since 1.6 (urgent!)

Topics: Core, General, Troubleshooting, Writing modules
Nov 8, 2012 at 11:50 AM

We updated recently to 1.6 and were about to update our live environment with it as well but then we noticed a CRITICAL issue (bug?) that occurs since the update.

You can download our example records @ https://dl.dropbox.com/u/23877279/Projects/Orchard/ChildParentIssue.zip (you'll find the migration code inside the records as well)

Problem is that this code

foreach (var data in contactRecord.Data.OrderBy(d => d.DataType).ThenBy(d => d.Priority)){ }

Generates the following exception

[SqlException (0x80131904): Invalid column name 'Child_id'.
Invalid column name 'Child_id'.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +2073486
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5064444
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2275
   System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +33
   System.Data.SqlClient.SqlDataReader.get_MetaData() +86
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +311
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +987
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141
   System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12
   System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader() +12
   NHibernate.AdoNet.AbstractBatcher.ExecuteReader(IDbCommand cmd) +292
   NHibernate.Loader.Loader.GetResultSet(IDbCommand st, Boolean autoDiscoverTypes, Boolean callable, RowSelection selection, ISessionImplementor session) +244
   NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) +186
   NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) +129
   NHibernate.Loader.Loader.LoadCollection(ISessionImplementor session, Object id, IType type) +228

[GenericADOException: could not initialize a collection: [Tacx.Dealers.Models.Data.Records.DealerContactRecord.Data#115][SQL: SELECT data0_.Child_id as Child6_1_, data0_.Id as Id1_, data0_.Id as Id134_0_, data0_.Value as Value134_0_, data0_.Priority as Priority134_0_, data0_.DataType as DataType134_0_, data0_.DealerContactRecord_id as DealerCo5_134_0_ FROM Tacx_Dealers_DealerContactDataRecord data0_ WHERE data0_.Child_id=?]]

Notice the last line : for some reason it is trying to query on the Child_id field @ Tacx_Dealers_DealerContactDataRecord while this property does NOT exist on this record! It exists on the DealerContactRecord!

The only thing that I could think of that might blow things up is that my DealerContactRecord has 2 properties (Parent / Child) that are also of the type DealerContactRecord. Another idea would be that properties with the name 'Child' are now somehow 'reserved'?

Any help is greatly appreciated as without a fix we cannot update our live site anymore :/

Coordinator
Nov 8, 2012 at 10:27 PM

Can you provide a complete module with a simple set of Records and the migration file to reproduce the issue ?

If you can't you might want to investigate by yourself, a solution being to add a new attribute to distinguish relationships in the generated mapping.

Nov 9, 2012 at 5:39 AM

I'll try to make a module that reproduces the issue.

I have no idea how to add such an attribute myself.

Nov 9, 2012 at 3:17 PM

We made a module with just the 3 models from the zip + a simple bit of code that attempts to query on a empty table triggers the above exception.

Problem is: Our TFS just went down right after my colleague said he made the test case, so I can't access it now.

I'll attach the test case to the issue (http://orchard.codeplex.com/workitem/19232) ASAP.

We really need a solution for this: Any help is appreciated, even if it would be an idea on how to work around this issue. Also, is no-one else experience this kind of issue since 1.6?

Developer
Nov 11, 2012 at 5:17 PM

It looks like you havent generated that column. Are you sure its generated? Can you look in the DB.

If its not there... then this piece of code is not being hit...

case 6: { schemaBuilder.AlterTable(typeof(DealerContactRecord).Name, table => table.AddColumn("Child_Id", DbType.Int32));

Nov 11, 2012 at 6:15 PM

I checked our db : the column exists. The problem is that it is generating an invalid query (tries to query the Child_id column on the wrong table)

Nov 12, 2012 at 8:00 AM
Edited Nov 12, 2012 at 9:02 AM

Added test case @work item.

Any aid is greatly appreciated (hack, workaround, ...) as this is a blocker for us (or we need to downgrade to 1.5 again.. painful + we'd be stuck on 1.5 then)

Nov 13, 2012 at 4:22 PM

Any update / aid ? This is still blocking us from updating our live website.

Workaround (in addition to a future perm fix) would also be OK.