[Dbix-class] constructing a query where fields are compared to each other

Peter Rabbitson rabbit+dbic at rabbit.us
Thu Oct 17 11:13:17 GMT 2013

On Wed, Oct 16, 2013 at 05:39:12PM -0700, Karen Etheridge wrote:
> On Wed, Oct 16, 2013 at 08:13:34PM -0400, Rob Kinyon wrote:
> > It's good you showed all the code because the extended comparison in a
> > join relationship isn't possible. (It's one of ribasushi's many
> > in-progress branches. Patches most definitely welcome here.)
> > 
> > However, this is definitely possible in a search. You have to use the trapdoor.
> > 
> >     ->search({ 1 => \"me.field_x - me.field_y" });
> > 
> > That is a reference to a string.
> A ha, so we drop down to literal SQL (via the scalar reference), but we
> can't do that in a join because we can't jam the $args->{foreign_alias}
> bits into the reference - it's then no longer literal SQL.

You can of course ;) You will have to forgive Rob, he has been out of 
the loop for a bit. The coderef-based relationship feature you correctly 
tried to use in your original email is what he meant by "one of 
ribasushi's branches", and that feature did land couple years ago.

The only thing you didn't do correctly was to use the coderef syntax 
where it is quite appropriate (there is a working draft for arbitrary OP 
expression *without* \'' business, but it will happen post Data::Query)

To answer your immediate question - you wanted:

__PACKAGE__->has_many(cats_dogs => 'MyApp::Schema::Dogs' =>
    sub {
        my $args = shift;
        \" $args->{foreign_alias}.field_x - $args->{foreign_alias}.field_y = 1";


More information about the DBIx-Class mailing list