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 »
10,640 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 »
10,640 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 »
10,640 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
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »