Can anyone explain why a column alias is being created in the WHERE clause in this scenario?
In attempting to learn the Criteria Builder – and Coldbox/ColdFusion in general – I noticed that when I refer to a property named “name” from my User model from within the .withProjections() then the SQL WHERE clause that is sent to SQL Server contains an alias instead of the actual column name.
So if I have a User model like this:
`
component persistent=“true” table=“Users” {
// Primary Key
property name=“ID” fieldtype=“id” column=“ID” generator=“native” setter=“false”;
// Properties
property name=“code” column=“Code” ormtype=“string”;
property name=“name” column=“Name” ormtype=“string”;
…
}
`
Then I do something like this in my Users handler:
var users = c.$and(c.restrictions.like("name", userName & "%"),c.restrictions.eq("status","Active")).withProjections(property="name, code").list();
The SQL that is generated is:
select this_.Name as y0_, this_.Code as y1_ from schema.Users this_ where ( y0_ like ? and this_.Status=? )
And I get a SQL Server error message:
Invalid column name 'y0_'.
But, if I alias that name property and change my Criteria to:
var users = c.$and(c.restrictions.like("name", userName & "%"),c.restrictions.eq("status","Active")).withProjections(property="name:userName, code").list();
Then the SQL is generated appropriately and I get the results I expect:
select this_.Name as y0_, this_.Code as y1_, from schema.Users this_ where ( this_.Name like ? and this_.Status=? )
Can anyone explain why a column alias is being created in the WHERE clause in the above scenario? It would be nice to know what to watch out for ahead of time rather than me having to alias every property or just wait until errors happen.
Similar revelations found here.
Thanks!
Wes