$self->related_resultset( 'user_role' ) returning App::DB::ResultSet

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

$self->related_resultset( 'user_role' ) returning App::DB::ResultSet

Bill Moseley
Interesting problem today.  Running 0.082800.

I don't have much to go on here, but maybe someone has seen this before.

This line of code in my "User" Result class was throwing an exception.  It's in a Moose builder:

return $self->related_resultset( 'user_role' )->my_method->count > 0;

saying:

Can't locate object method "my_method" via package "App::DB::ResultSet"   

That method "my_method" is not in App::DB::ResultSet.  It is in App::DB::ResultSet::UserRole.
I'm expecting that related_resultset( 'user_role' ) would return App::DB::ResultSet::UserRole.

And, well, it does when run outside of mod_perl.  And now, after quite a few Apache restarts and adding some debugging code, it is now returning App::DB::ResultSet::UserRole.

Has anyone had a case where related_resultset() returns a ResultSet object?


FWIW, the relationship is defined as so in Result::User:

__PACKAGE__->has_many(
    'user_role',
    'App::DB::Result::UserRole',
    { 'foreign.user_id' => 'self.id' },
);



--
Bill Moseley
[hidden email]

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: $self->related_resultset( 'user_role' ) returning App::DB::ResultSet

Francisco Obispo
The result of

$self->related_resultset( 'user_role' )

is a App::DB::ResultSet

Since it’s declared as ‘has_many’ you have to either wrap it in a
loop to extract the individual APP::DB::Result::UserRole so you can call
->my_method

(or use ->single)


On 15 Jul 2015, at 15:08, Bill Moseley wrote:

> Interesting problem today.  Running 0.082800.
>
> I don't have much to go on here, but maybe someone has seen this
> before.
>
> This line of code in my "User" Result class was throwing an exception.
> It's in a Moose builder:
>
> return $self->related_resultset( 'user_role' )->my_method->count > 0;
>
>
> saying:
>
> Can't locate object method "my_method" via package
> "App::DB::ResultSet"
>
> That method "my_method" is not in App::DB::ResultSet.  It is in
> App::DB::ResultSet::UserRole.
> I'm expecting that related_resultset( 'user_role' ) would return
> App::DB::ResultSet::UserRole.
>
> And, well, it does when run outside of mod_perl.  And now, after quite
> a
> few Apache restarts and adding some debugging code, it is now
> returning
> App::DB::ResultSet::UserRole.
>
> Has anyone had a case where related_resultset() returns a ResultSet
> object?
>
>
> FWIW, the relationship is defined as so in Result::User:
>
> __PACKAGE__->has_many(
>  'user_role',
>  'App::DB::Result::UserRole',
>  { 'foreign.user_id' => 'self.id' },
> );
>
>
>
> --
> Bill Moseley
> [hidden email]
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive:
> http://www.grokbase.com/group/dbix-class@...

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: $self->related_resultset( 'user_role' ) returning App::DB::ResultSet

Bill Moseley


On Wed, Jul 15, 2015 at 3:15 PM, Francisco Obispo <[hidden email]> wrote:
The result of

$self->related_resultset( 'user_role' )

is a App::DB::ResultSet

But it should return an App::DB::ResultSet::UserRole object.  "my_method" is in the App::DB::ResultSet::UserRole class.

return $self->related_resultset( 'user_role' )->my_method->count > 0;

When I printed Dumper $self->{related_resultsets} (in the related_resultset() method) when it was NOT working I was getting:

Note the class:

          'user_role' => bless( {
                                  'pager' => undef,
                                  'attrs' => {
                                               'is_depends_on' => 0,
                                               'cascade_copy' => 0,
                                               'accessor' => 'multi',
                                               'alias' => 'me',
                                               'bind' => 'ARRAY(0x7fdf9ac59198)',
                                               'join_type' => 'LEFT',
                                               'where' => 'HASH(0x7fdf9ac58fd0)',
                                               'cascade_delete' => 0
                                             },
                                  'cond' => $VAR1->{'user_role'}{'attrs'}{'where'},
                                  '_result_class' => 'App::DB::Result::UserRole',
                                  'result_source' => bless( {
                                                              '_columns' => 'HASH(0x7fdf85857f40)',
                                                              'schema' => $VAR1->{'user_pref'}{'result_source'}{'schema'},
                                                              '_unique_constraints' => 'HASH(0x7fdf9696ffc8)',
                                                              '_relationships' => 'HASH(0x7fdf8585b0f8)',
                                                              'result_class' => 'Appi::DB::Result::UserRole',
                                                              'source_name' => 'UserRole',
                                                              'resultset_class' => 'App::DB::ResultSet::UserRole',
                                                              '_columns_info_loaded' => 0,
                                                              'resultset_attributes' => 'HASH(0x7fdf85857a48)',
                                                              '_primaries' => 'ARRAY(0x7fdf93dcb030)',
                                                              '_ordered_columns' => 'ARRAY(0x7fdf858417e8)',
                                                              'name' => 'user_role'
                                                            }, 'DBIx::Class::ResultSource::Table' )
                                }, 'App::DB::ResultSet' )
 


And then when it is working correctly the Dump shows this:

          'user_role' => bless( {
                                  'result_source' => bless( {
                                                              'source_name' => 'UserRole',
                                                              'name' => 'user_role',
                                                              '_columns_info_loaded' => 0,
                                                              '_primaries' => 'ARRAY(0x4ceaea0)',
                                                              'schema' => 'App::DB=HASH(0x2c458d0)',
                                                              'result_class' => 'App::DB::Result::UserRole',
                                                              '_relationships' => 'HASH(0x783f628)',
                                                              'resultset_attributes' => 'HASH(0x7834010)',
                                                              'resultset_class' => 'App::DB::ResultSet::UserRole',
                                                              '_ordered_columns' => 'ARRAY(0x7825410)',
                                                              '_columns' => 'HASH(0x7837d20)',
                                                              '_unique_constraints' => 'HASH(0x4d17b40)'
                                                            }, 'DBIx::Class::ResultSource::Table' ),
                                  'attrs' => {
                                               'bind' => 'ARRAY(0x7b1ace0)',
                                               'cascade_delete' => 0,
                                               'accessor' => 'multi',
                                               'cascade_copy' => 0,
                                               'is_depends_on' => 0,
                                               'alias' => 'me',
                                               'where' => 'HASH(0x7b71fe0)',
                                               'join_type' => 'LEFT'
                                             },
                                  'pager' => undef,
                                  '_result_class' => 'App::DB::Result::UserRole',
                                  'cond' => $VAR1->{'user_role'}{'attrs'}{'where'}
                                }, 'App::DB::ResultSet::UserRole' )
        };

--
Bill Moseley
[hidden email]

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: $self->related_resultset( 'user_role' ) returning App::DB::ResultSet

Bill Moseley
Ok, I have an explanation.

We did a release and App::DB::ResultSet::UserRole is a new class.   But, we cache User objects in Memcached (as a DBIC row).

And the old code did this:

    $user->search_related( 'user_role', { ... } );

which then populated $user->{related_resultsets}{user_role} with the (default?) App::DB::Result class instance (and saved it in Memcached).

So, when we rolled out the new code it was using cached $user objects so $user->{related_resultsets}{user_role} was the base ResultSet object which didn't have the "my_method" method.

So, it may be as simple as delete $user->{related_resultsets} after fetching the $user object from cache.   Can't be that expensive to look up the relationships.

Or we could be disruptive and blow away the cache at release time.   But, it's not a problem that happens very often.


I wonder if it makes sense for DBIC to not save related_resultset in objects if there isn't a specific one -- which would have prevented this.   Seems like quite an edge-case.



On Wed, Jul 15, 2015 at 4:45 PM, Bill Moseley <[hidden email]> wrote:


On Wed, Jul 15, 2015 at 3:15 PM, Francisco Obispo <[hidden email]> wrote:
The result of

$self->related_resultset( 'user_role' )

is a App::DB::ResultSet

But it should return an App::DB::ResultSet::UserRole object.  "my_method" is in the App::DB::ResultSet::UserRole class.

return $self->related_resultset( 'user_role' )->my_method->count > 0;

When I printed Dumper $self->{related_resultsets} (in the related_resultset() method) when it was NOT working I was getting:

Note the class:

          'user_role' => bless( {
                                  'pager' => undef,
                                  'attrs' => {
                                               'is_depends_on' => 0,
                                               'cascade_copy' => 0,
                                               'accessor' => 'multi',
                                               'alias' => 'me',
                                               'bind' => 'ARRAY(0x7fdf9ac59198)',
                                               'join_type' => 'LEFT',
                                               'where' => 'HASH(0x7fdf9ac58fd0)',
                                               'cascade_delete' => 0
                                             },
                                  'cond' => $VAR1->{'user_role'}{'attrs'}{'where'},
                                  '_result_class' => 'App::DB::Result::UserRole',
                                  'result_source' => bless( {
                                                              '_columns' => 'HASH(0x7fdf85857f40)',
                                                              'schema' => $VAR1->{'user_pref'}{'result_source'}{'schema'},
                                                              '_unique_constraints' => 'HASH(0x7fdf9696ffc8)',
                                                              '_relationships' => 'HASH(0x7fdf8585b0f8)',
                                                              'result_class' => 'Appi::DB::Result::UserRole',
                                                              'source_name' => 'UserRole',
                                                              'resultset_class' => 'App::DB::ResultSet::UserRole',
                                                              '_columns_info_loaded' => 0,
                                                              'resultset_attributes' => 'HASH(0x7fdf85857a48)',
                                                              '_primaries' => 'ARRAY(0x7fdf93dcb030)',
                                                              '_ordered_columns' => 'ARRAY(0x7fdf858417e8)',
                                                              'name' => 'user_role'
                                                            }, 'DBIx::Class::ResultSource::Table' )
                                }, 'App::DB::ResultSet' )
 


And then when it is working correctly the Dump shows this:

          'user_role' => bless( {
                                  'result_source' => bless( {
                                                              'source_name' => 'UserRole',
                                                              'name' => 'user_role',
                                                              '_columns_info_loaded' => 0,
                                                              '_primaries' => 'ARRAY(0x4ceaea0)',
                                                              'schema' => 'App::DB=HASH(0x2c458d0)',
                                                              'result_class' => 'App::DB::Result::UserRole',
                                                              '_relationships' => 'HASH(0x783f628)',
                                                              'resultset_attributes' => 'HASH(0x7834010)',
                                                              'resultset_class' => 'App::DB::ResultSet::UserRole',
                                                              '_ordered_columns' => 'ARRAY(0x7825410)',
                                                              '_columns' => 'HASH(0x7837d20)',
                                                              '_unique_constraints' => 'HASH(0x4d17b40)'
                                                            }, 'DBIx::Class::ResultSource::Table' ),
                                  'attrs' => {
                                               'bind' => 'ARRAY(0x7b1ace0)',
                                               'cascade_delete' => 0,
                                               'accessor' => 'multi',
                                               'cascade_copy' => 0,
                                               'is_depends_on' => 0,
                                               'alias' => 'me',
                                               'where' => 'HASH(0x7b71fe0)',
                                               'join_type' => 'LEFT'
                                             },
                                  'pager' => undef,
                                  '_result_class' => 'App::DB::Result::UserRole',
                                  'cond' => $VAR1->{'user_role'}{'attrs'}{'where'}
                                }, 'App::DB::ResultSet::UserRole' )
        };

--
Bill Moseley
[hidden email]



--
Bill Moseley
[hidden email]

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...