vanstyn / RapidApp

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

DBIC drops joins needed by virtual columns of related tables #189

Closed nrdvana closed 3 years ago

nrdvana commented 3 years ago

Suppose you have a main table Album with relation artist to table Artist where you have added a virtual column album_count.

package Album;
...
...
__PACKAGE__->belongs_to( artist => 'MySchema::Result::Artist', { id => 'artist_id' };
  virtual_columns => {
    Artist => {
      album_count => {
        sql => q/ SELECT COUNT(*) FROM album WHERE album.artist_id = self.id /
      }
    }
  }

Now suppose you select columns for an Album grid, and choose artist.album_count without selecting any other fields from artist. DBIC strips out the join to the artist table because no concrete columns were using it, and then the SQL fails because it generated

SELECT ..., (SELECT COUNT(*) FROM album WHERE album.artist_id = artist.id )
FROM album me
WHERE ...

and artist is not defined.

This could be fixed by the code checking for the case where a virtual column is the only column using a join, and then just add some un-needed column (like the primary key) so DBIC doesn't strip it out.

  if ($join_is_only_used_by_virtual_column) {
    $rs= $rs->search_rs(undef, { join => "artist", "+columns" => { artist__id => "artist.id" } })
  }

Or, maybe just unconditionally add the primary columns of the related table any time a virtual column is used?

vanstyn commented 3 years ago

@nrdvana - if this has been fixed already in the PR by either you or @deven, please close this issue, or let me know if it is still an issue