I'm using DBIx::Class and I have two Schemas:
use utf8;
package MyApp::Schema::Result::Person;
use Moose;
use MooseX::NonMoose;
use MooseX::MarkAsMethods autoclean => 1;
extends 'DBIx::Class::Core';
__PACKAGE__->table("person");
__PACKAGE__->add_columns(
"id",
{ data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
);
__PACKAGE__->has_many(
"addresses",
"MyApp::Schema::Result::Address",
{ "foreign.person_id" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 },
);
1;
And:
use utf8;
package MyApp::Schema::Result::Address;
use Moose;
use MooseX::NonMoose;
use MooseX::MarkAsMethods autoclean => 1;
extends 'DBIx::Class::Core';
__PACKAGE__->table("address");
__PACKAGE__->add_columns(
"id",
{ data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
"person_id",
{ data_type => "integer", is_foreign_key => 1, is_nullable => 1 },
);
__PACKAGE__->belongs_to(
"person",
"MyApp::Schema::Result::Person",
{ id => "person_id" },
{
is_deferrable => 0,
join_type => "LEFT",
on_delete => "NO ACTION",
on_update => "NO ACTION",
},
);
1;
What I am trying to do is add multiple addresses at once with a person object. I am doing that like so:
my $person = $c->model('DB::Person')->new_result({});
$person->addresses([
{
id => 1,
person_id => 1,
},
{
id => 2,
person_id => 1,
},
]);
$person->insert;
I followed this format from this article, but it doesn't seem to work. Only the person row gets inserted, but the addresses associated with it do not. I've also tried setting addresses
to an arrayref of MyApp::Schema::Result::Address
objects before inserting, but that doesn't work either. Does anyone know what I'm doing wrong? I don't get any errors, it just doesn't insert the addresses. In the article they use create instead of insert. Is it because of this? If so, is there a way to do this using insert or update?