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

Peter Rabbitson rabbit+dbic at rabbit.us
Sun Feb 4 18:14:53 GMT 2018


On 02/04/2018 02:09 PM, Roman Daniel wrote:
> 
> Also your example with CD and liner_notes is very different from mine. 

You are absolutely correct. Apologies, I misread your question 
pre-coffe. Your *actual* relationship-type is "belongs_to", see the same 
RT comment I linked earlier.

> All I wanted from multi-creation was to create related record first 
> without any attempt to find it before (and reuse its id). The more I 
> look into DBIC code, the more it seems to me that is impossible to 
> define relationships that way and I will need to create the related 
> object first manually. Am I correct?

Almost. "multicreate" heavily depends on how the "directionality" of the 
structure you give it maps to the directions of the dependency-type ( 
belongs_to vs might_have/has_many ).

When your "nested" portion of the multicreate structure is "a thing that 
I refer to which can bestandalone" - a find is fired before the create 
invariably ( what you are observing ).

You still *can* use the multicreate functionality by reversing the 
order: create an address that includes as a sub-structure the customer. 
Then everything will work as you expect. If this is undesirable ( your 
main return value will be the address ) then you can could:
my $cust = ->result("Address")->create( \%addr_data )->create_related( 
\%cust_data )

Hope this helps!



More information about the DBIx-Class mailing list