QueryNew() Type Casting Issues With Manually Altered Queries

Posted April 19, 2006 at 8:12 AM by Ben Nadel

Tags: ColdFusion

I am having this really strange problem involving a query of queries that I have never encountered before. Basically I am building a query with the QueryNew() method:

<cfset REQUEST.SearchQuery = QueryNew(
"title, date, preview, link, score",
"VARCHAR, DATE, VARCHAR, VARCHAR, INTEGER"
) />

Then I build the query based on other queries. If I display this one, at this point, it's totally fine. After building the query I do a QofQ like so:

<cfquery name="REQUEST.SearchQuery" dbtype="query">
SELECT title
FROM REQUEST.SearchQuery
ORDER BY
score DESC,
[date] DESC
</cfquery>

This sometimes throws the following error: " Error casting an object of type to an incompatible type. This usually indicates a programming error in Java, although it could also mean you have tried to use a foreign object in a different way than it was designed."

I have narrowed it down to the line of code that causes the "casting" issue. It happens when I update the SCORE value. It really doesn't matter how it is updated as long as it is set by a method call.

For example, the following cause NO ERROR:

<cfset REQUEST.SearchQuery[ "score" ][ REQUEST.SearchQuery.CurrentRow ] = 100 />
<cfset REQUEST.SearchQuery[ "score" ][ REQUEST.SearchQuery.CurrentRow ] = 0 />
<cfset REQUEST.SearchQuery[ "score" ][ REQUEST.SearchQuery.CurrentRow ] =
REQUEST.SearchQuery[ "score" ][ REQUEST.SearchQuery.CurrentRow ] />

However, the following all cause error:

<cfset REQUEST.SearchQuery[ "score" ][ REQUEST.SearchQuery.CurrentRow ] = ArrayLen( arrCriteria ) />
<cfset REQUEST.SearchQuery[ "score" ][ REQUEST.SearchQuery.CurrentRow ] =
Max( 5, REQUEST.SearchQuery[ "score" ][ REQUEST.SearchQuery.CurrentRow ] ) />

Heck, even this causes errors:

REQUEST.SearchQuery[ "score" ][ REQUEST.SearchQuery.CurrentRow ] =
Max( REQUEST.SearchQuery[ "score" ][ REQUEST.SearchQuery.CurrentRow ],
REQUEST.SearchQuery[ "score" ][ REQUEST.SearchQuery.CurrentRow ]
) />

Its like any function call that returns a number corrupts the SCORE value for the QofQ.

NOW, EVEN MORE CRAZY, if I take out the data types in the QueryNew() method, so that it is just:

<cfset REQUEST.SearchQuery = QueryNew(
"title, date, preview, link, score",
) />

, the whole thing works like a charm. CRAZY! I have posted this to the CFTalk list... can't wait to see what people's solutions are.




Reader Comments

Jun 19, 2007 at 6:20 AM // reply »
1 Comments

Ben,
I had the same issue in a different context. I was attempting to add boolean columns to 2 queries using the QueryAddColumn function and specifying the column type as BIT. From this I was UNIONing the QUERY results together using QofQ. The error disappeared after I removed the BIT casting parameter from the QueryAddColumn functions with your recommendation.

Thanks for posting this. You saved me a ton of time with that coding gem.

Thans,
Mike


Jun 19, 2007 at 7:29 AM // reply »
11,238 Comments

@Mike,

Glad to help.


Jun 19, 2008 at 5:30 PM // reply »
1 Comments

Just had this after some seemingly minor changes to a page... it turns out, moving a query object from the unnamed scope to the session makes then entire structure more type-sensitive.

I got a "Error casting..." message for:

SELECT * FROM session.st_invoice.query WHERE division_code > 0;

...despite that...

SELECT * FROM st_invoice.query WHERE division_code > 0;

...worked fine, having changed NOTHING else in the code except moving the QueryNew and all its relevant commands to write directly to the session variable.

It took me about 5 minutes to figure out What, but I spent the better part of the day on "why" before coming to the realization that if I set division code with JavaCast, it worked just fine...


Jun 25, 2008 at 11:32 PM // reply »
11,238 Comments

@Christopher,

That is odd. I wonder why the scope change would have any change at all in the sensitivity. I didn't see from your code, though, where the JavaCast() made a difference.


Feb 26, 2009 at 6:08 AM // reply »
1 Comments

You are the man Ben! I was absolutely stumped by this crappy error message and my experience with QoQ is seriously lacking. Still got inspiration when I read about JavaCast() on numbers. Solved the error.

I wish the Coldfusion had proper debugging tools like Java.

any way thank you very much and to everyone else who uses this site.

Just for the error was caused by a variable I was using that had a number stored. However the Cold fusion debugger only complained when I tried to sort my newly created query. Oh well, I won't forget this any time soon.

thanks

Eish


Feb 26, 2009 at 8:00 AM // reply »
11,238 Comments

@Eish,

Glad to help out.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 19, 2013 at 2:31 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
It's funny really just how well that image describes the way I would imagine most people that go with angular for some project is. I have had a similar roller-coaster ride with it as well, but not qu ... read »
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools