Subject: | relation id is not initialized after add_to_$relname |
I have next relations:
__PACKAGE__->table('customers');
__PACKAGE__->has_many('packages', 'SafeVPN::DB::Result::Package', 'customer_id', {cascade_delete => 0});
__PACKAGE__->table('packages');
__PACKAGE__->belongs_to('customer' => 'SafeVPN::DB::Result::Customer', 'customer_id');
__PACKAGE__->has_many('destinations', 'SafeVPN::DB::Result::Destination', 'package_id', {cascade_delete => 0});
__PACKAGE__->table('destinations');
__PACKAGE__->belongs_to('package' => 'SafeVPN::DB::Result::Package', 'package_id');
my %package = (
customer_id => $customer->id,
...
destinations => [ { address_id => 33717 } ]
);
Running:
$DBIC->resultset('Package')->create( \%package );
OR
my $package = $customer->add_to_packages(\%package);
result:
Can't call method "tarif" on an undefined value at /home/feelsafe/public_html/lib/SafeVPN/DB/Result/Destination.pm *line 73*
at this file line 73 I have:
sub new {
my ( $class, $attrs ) = @_;
my $self = $class->next::method($attrs);
if( <some cond> ) {
$val = $self->package->tarif->type; #<<<< line 73
<do calculation here, based on type, for init some null columns>
}
return $self;
}
As this is destination code, so package at this point is MUST BE created already.
so in your code maybe happen next
First suggestion:
package is created in other transaction and destination doest not see it
This is wrong suggestion, because of I must get DB FK violation
Second suggestoin:
after creating package row, you do not update internal structures or you do not fill package_id
This is wrong sugggestion, because of this work fine: http://cpansearch.perl.org/src/FREW/DBIx-Class-0.08115/t/multi_create/torture.t
Third suggestion:
When I do read in mine Destination.pm@new. The read transaction does not see underlying changed, that were done in write transaction.