Subject: | Lookup related field does not work: $row->User->email |
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 textDBG> $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,
},
],
})
```