vanstyn / RapidApp

Turnkey ajaxy webapps
http://rapi.io
Other
48 stars 15 forks source link

Filters on Multi-Relationship columns not working with SQLite #10

Closed vanstyn closed 11 years ago

vanstyn commented 11 years ago

Multi Relationship columns show a count of related rows, which behave as an integer and are supposed to be able to be filtered as such (i.e. "greater/less than N related rows").

Internally, this is put into a HAVING clause in the query. However, the generated SQL puts the value in quotes (i.e. HAVING foo > '3' instead of HAVING foo > 3) and in SQLite this is the difference from returning the correct rows or returning no rows.

Not sure yet if this is a problem in RapidApp/DbicLink2 or DBIx::Class

Bug in RapidApp v0.99019

vanstyn commented 11 years ago

Here is an example generated SQL query from trying to search for the multi-relationship albums greater than 3 (as generated by DBIC_TRACE=1 DBIC_TRACE_PROFILE=console)

SELECT ( "me"."artistid" ) AS "artistid", ( "me"."name" ) AS "name", ( 
    SELECT COUNT( * ) 
      FROM "Album" "inner" 
    WHERE "inner"."artistid" = me.artistid
   ) AS "albums" 
  FROM "Artist" "me" 
GROUP BY "me"."artistid" 
HAVING "albums" > '3' 
ORDER BY "albums" DESC 
  LIMIT '25'

This query returns 0 rows in SQLite, but if HAVING "albums" > '3' is changed to HAVING "albums" > 3 the expected rows are returned.

The database in use for this is from RA-ChinookDemo

ribasushi commented 11 years ago

DBIC_TRACE_PROFILE=console does not reflect the SQL sent to the server - all the magenta highlighted values are actually placeholders.

The actual reason for this issue is SQLite not knowing what a type is: https://metacpan.org/module/DBD::SQLite#Functions-And-Bind-Parameters

DBIC workaround: having => { foo => { '>' => \[ '?', [ \int => 3 ] ] } } or just having => [ 'foo > ?', [ \int => 3 ]] or something along these lines...