[Dbix-class] How to avoid find when create related records

Peter Rabbitson rabbit+dbic at rabbit.us
Sun Feb 4 09:19:31 GMT 2018


On 02/03/2018 05:05 PM, Roman Daniel wrote:

>      __PACKAGE__->has_one(
>          'address',
>          'MyApp::Schema::Result::Address',
>          { 'foreign.address_id' => 'self.address_id' }
>      );
> 

The correct relationship here is `might_have`, this is what confuses the 
entire multi-creation chain. Please read this comment ( not the whole 
thread ) for more info: 
https://rt.cpan.org/Ticket/Display.html?id=83712#txn-1187773


>      $schema->resultset('Customer')->create(
>          {
>              name    => $_,
>              address => {
>                  street => 'Axmanova 11',
>                  city   => 'Brno',
>              }
>          }
>        )

rabbit at Ahasver:~/devel/dbic$ grep -C2 liner_notes 
t/lib/DBICTest/Schema/CD.pm
# tests oddball legacy syntax
__PACKAGE__->might_have(
     liner_notes => 'DBICTest::Schema::LinerNotes', undef,
     { proxy => [ qw/notes/ ] },
);

rabbit at Ahasver:~/devel/dbic$ perl -It/lib -Ilib -MDBICTest -e '
   my $s = DBICTest->init_schema;
   $s->storage->debug(1);
   $s->resultset("CD")->create({
     title => "foo",
     year => 2000,
     artist => 1,
     liner_notes => { notes => "foo" }
   })
'
BEGIN WORK
INSERT INTO cd ( artist, title, year) VALUES ( ?, ?, ? ): '1', 'foo', '2000'
INSERT INTO liner_notes ( liner_id, notes) VALUES ( ?, ? ): '6', 'foo'
COMMIT


Cheers!



More information about the DBIx-Class mailing list