nhibernate-core icon indicating copy to clipboard operation
nhibernate-core copied to clipboard

NH-3513 - Invalid SQL with property-ref on property using a formula

Open nhibernate-bot opened this issue 7 years ago • 9 comments

Jeff Becker created an issue:

Framework:4.5 Dialect: MsSql2008Dialect When a many-to-one relationship with a property-ref targets a property which has a formula invalid sql as shown below is generated:

SELECT account0_.Id as Id1_0_, account0_.Name as Name1_0_, convert(int,substring(account0_.[oldaccountnumber],3,8)) as formula0_0_ FROM Account account0_ 
WHERE  account0_.=@p0 -- Empty alias missing formula on try to load many-to-one association

nhibernate-bot avatar Oct 13 '17 00:10 nhibernate-bot

Fix should adjust AbstractEntityPersister.CreateUniqueKeyLoaders to take into account formulas (propertyColumnFormulaTemplates)

bahusoid avatar Sep 01 '20 09:09 bahusoid

Hi i m new here. I would like to help on this one

PapisKang avatar May 16 '23 22:05 PapisKang

Hi,

You are welcome to do so. See Contributing to get started.

fredericDelaporte avatar May 21 '23 17:05 fredericDelaporte

https://github.com/nhibernate/nhibernate-core/pull/3309 I have some troubles by doing the test case. I talked about it a little bit on the PR . But overall i do not understand how can we do a many to one relationship targetting a formula. Because what i understood is formula are evaluations and i checked the database there is no column for the formula. So when i try to do the many to one relationship with a formula, i got this error : Message:  OneTimeSetUp: NHibernate.HibernateException : Column 'Customer.Id' is not the same data type as referencing column 'Orders.Customer1' in foreign key 'FK_C00737E9'. Could not create constraint or index. See previous errors. ----> System.Data.SqlClient.SqlException : Column 'Customer.Id' is not the same data type as referencing column 'Orders.Customer1' in foreign key 'FK_C00737E9'. Could not create constraint or index. See previous errors. Data: HelpLink.ProdName: Microsoft SQL Server HelpLink.ProdVer: 16.00.1000 HelpLink.EvtSrc: MSSQLServer HelpLink.EvtID: 1778 HelpLink.BaseHelpUrl: https://go.microsoft.com/fwlink HelpLink.LinkId: 20476 image

PapisKang avatar May 27 '23 02:05 PapisKang

@PapisKang the original issue actually has a test case. I've added link it to the description.

hazzik avatar May 27 '23 02:05 hazzik

@hazzik I tried to report the Test Case that have been made before but as i was afraid off, it is not possible anymore to specify both column and formula image

Message:  OneTimeSetUp: NHibernate.MappingException : Could not compile the mapping document: NHibernate.Test.NHSpecificTest.GH3513.Mappings.hbm.xml ----> NHibernate.MappingException : On OldAccountNumber property: specifying both column and formula attributes is invalid. Please specify only one of them, or use sub-elements

PapisKang avatar May 27 '23 03:05 PapisKang

Just remove column attribute in this case.

Please specify only one of them, or use sub-elements

But in general case to use both use sub-elements as suggested. Example: https://github.com/nhibernate/nhibernate-core/blob/90d50c03475628d10336cbad368f071375056fef/src/NHibernate.Test/TypedManyToOne/Customer.hbm.xml#L32-L35

bahusoid avatar May 27 '23 07:05 bahusoid

@bahusoid Thanks for the help. I finally succeeded to report the test case and reproduce the error. actual-sql-query: SELECT account0_.Id as id1_1_0_, account0_.Name as name2_1_0_, convert(int,substring(account0_.[Oldaccountnumber],3,8)) as formula0_0_ FROM Account account0_ WHERE account0_.=@p0

I dont know if an explicit assertion is needed for the exception.

PapisKang avatar May 27 '23 15:05 PapisKang

Hi everyone, I just want to let you know that I'm willing to address and resolve this issue. Fixing the bug should be the next step, if I'm not mistaken, as test cases have already been created.

PapisKang avatar Apr 23 '24 20:04 PapisKang