Skip Menu |
 

This queue is for tickets about the DBIx-Class CPAN distribution.

Report information
The Basics
Id: 131624
Status: open
Priority: 0/
Queue: DBIx-Class

People
Owner: Nobody in particular
Requestors: KES [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Lookup related field does not work: $row->User->email
Download (untitled) / with headers
text/plain 2.5k
Doing $row->User->email cause error: Can't call method "email" on an undefined value this error happen when $row is: ``` Show quoted text
DBG>$row
App::Schema::Result::Right { _column_data => { id => 151, role_id => 2, user_id => 150, }, _in_storage => 1, _inflated_column => {}, _relationship_data => IGNORED, _result_source => IGNORED, related_resultsets => IGNORED, } ``` And its relationship data looks like. (Notice `User => undef`): ``` Show quoted text
DBG> $row->{ _relationship_data}
{ Role => App::Schema::Result::Role { _column_data => { name => user, }, _in_storage => 1, _result_source => IGNORED, }, User => undef, } ``` For previous $row in my `while( my $row = $rows->next ) { ` it was: ``` $self->{_relationship_data} { Role => App::Schema::Result::Role { _column_data => { name => user, }, _in_storage => 1, _result_source => IGNORED, }, User => App::Schema::Result::User { _column_data => { email => some@mail, }, _in_storage => 1, _result_source => IGNORED, }, } ``` The target query for $rows is: ``` $rows->as_query \[ (SELECT "me"."id", "me"."user_id", "me"."role_id", "User"."email", "Role"."name" FROM "users_roles" "me" JOIN "users" "User" ON "User"."id" = "me"."user_id" JOIN "roles" "Role" ON "Role"."id" = "me"."role_id" ORDER BY "me"."id" DESC LIMIT ? OFFSET ?), [ { sqlt_datatype => integer, }, 20, ], [ { sqlt_datatype => integer, }, 20, ], ] ``` We can check that `user_id => 150` exists: ``` $c->db->resultset( 'User' )->find( 150 ); App::Schema::Result::User { _column_data => { activation => undef, email => undef, fax => undef, firstname => Alain, id => 150, }, _in_storage => 1, _result_source => IGNORED, } ``` And `email` is available if I write: $c->db->resultset( 'Right' )->find( 151 )->User->email undef Also: $c->db->resultset( 'Right' )->find( 151 )->User->firstname Alian But: $row->User->firstname Can't call method "firstname" on an undefined value at ... **WHY** DBIx::Class by mistake count that whole relation `User` does not exists because its field `email` is undef. **TODO** DBIx::Class should allow to access to fields of related table even if one of them is `undef` additional info: rows is constructed as: ``` ->search_rs( {}, { columns => [ me.id, me.user_id, me.role_id, User.email, Role.name, ], order_by => [ { -desc => me.id, }, ], }) ```
Download (untitled) / with headers
text/plain 383b
On Fri Jan 31 11:33:34 2020, KES wrote: Show quoted text
> Doing $row->User->email cause error: > > Can't call method "email" on an undefined value >
Thank you very much for the detailed report. It is however missing (arguably) the most important portion: what file/line did you get the above error at? Also which version of DBIC ( so I can match the file/error against the actual spot ) Thanks!
Download (untitled) / with headers
text/plain 682b
DBIx::Class - 0.082841 The error is at my file where I do $row->User->email. So file:line will not be helpful for you. The temporary workaround is to select additionally id field (that is guaranted is not null) so relationship now is correct Thus columns now are: [qw/ me.id me.user_id me.role_id User.email User.id Role.name /] and $row->{ _relationship_data } { Role => App::Schema::Result::Role { _column_data => { name => user, }, _in_storage => 1, _result_source => IGNORED, }, User => App::Schema::Result::User { _column_data => { email => undef, id => 150, }, _in_storage => 1, _result_source => IGNORED, }, }
Download (untitled) / with headers
text/plain 576b
The expected relationship_data when I do not select User.id is: $row->{ _relationship_data } { Role => App::Schema::Result::Role { _column_data => { name => user, }, _in_storage => 1, _result_source => IGNORED, }, User => App::Schema::Result::User { _column_data => { email => undef, }, _in_storage => 1, _result_source => IGNORED, }, } instead of actual: { Role => App::Schema::Result::Role { _column_data => { name => user, }, _in_storage => 1, _result_source => IGNORED, }, User => undef, }
Download (untitled) / with headers
text/plain 298b
On Fri Jan 31 11:50:00 2020, KES wrote: Show quoted text
> DBIx::Class - 0.082841 > > > The error is at my file where I do $row->User->email. So file:line > will not be helpful for you. >
My bad, I misunderstood your original example. That should be all I need. I'll get back to you some time over the weekend.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.