0

For some reason, when I specify "random" in my order clause of findAll(), CFWHeels throws an error:

Wheels.ColumnNotFound

Wheels looked for the column mapped to the random property but couldn't find it in the database table. Suggested action

Verify the order argument and/or your property to column mappings done with the property method inside the model's init method to make sure everything is correct.

I have used calculated properties before, but this shouldn't be looking for any of that with regard to "random" ordering.

<cffunction name="random">

    <cfparam name="params.page" default="1" />
    <cfparam name="params.pageQuantity" default="5" />

    <cfset posts = model("post").findAll(

        select = "

            posts.postID,
            postTitle,
            postPoints,
            postAuthority,
            postCreated,

            postCommentCount,
            postUpVoteCount,
            postDownVoteCount,

            users.userID,
            userName,

            categories.categoryID,
            categoryTitle,
            categoryToken",

        include     = "user,category", 
        order       = "random",
        page        = params.page,
        perPage     = params.pageQuantity

    ) />

</cffunction>

Does this possibly have something to do with using a select statement?

Would appreciate any help.

Many thanks, Michael.

Jeromy French
  • 11,812
  • 19
  • 76
  • 129
Michael Giovanni Pumo
  • 14,338
  • 18
  • 91
  • 140
  • What sql does this generate? What happens if you run that sql directly against your db? – Dan Bracuk Jan 24 '13 at 16:27
  • @DanBracuk I am unable to view the SQL generated, as this is a CFWheels error page and not a Railo error - is there any way I can do this? – Michael Giovanni Pumo Jan 24 '13 at 17:00
  • It might be a CFWheels error page but it sure looks like a database error. Does your database support "order by random" when random is neither a fieldname nor an alias from your select clause? – Dan Bracuk Jan 24 '13 at 17:04
  • @DanBracuk I am using Railo with MySQL. I just tried running a simple random select on the DB directly and it works just fine. "SELECT * FROM posts ORDER BY rand()". Any ideas? Does CFWheels support this on MySQL? Or is there a better way altogether to achieve this? – Michael Giovanni Pumo Jan 24 '13 at 17:20
  • If "order by rand()" works in mySQL, the first thing I would try is "order = 'rand()'" in the wheels function. – Dan Bracuk Jan 24 '13 at 17:25
  • @DanBracuk Weirdly, that solved it! Thank you. I assumed CFWheels was using an aliased keyword of its own called "random" that was cross-database, since that is what is shown in the docs for this. Using order="rand()" works as expected! Turn your comment into an answer and I'll mark it as correct! – Michael Giovanni Pumo Jan 24 '13 at 17:39

1 Answers1

3

As established in the commments.

If order by rand() works in mySQL, the first thing I would try is order = 'rand()' in the wheels function.

Peter Boughton
  • 110,170
  • 32
  • 120
  • 176
Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43