You can feed the ->create method of DBIx::Class with a recursive datastructure and have the related records created. Unfortunately you cannot do a similar thing with update_or_create. This module tries to fill that void until DBIx::Class has an api itself.

It is a base class for DBIx::Class::ResultSets providing the method recursive_update which works just like update_or_create but can recursively update or create result objects composed of multiple rows. All rows need to be identified by primary keys so you need to provide them in the update structure (unless they can be deduced from the parent row. For example a related row of a belongs_to relationship). If any of the primary key columns are missing, a new row will be created, with the expectation that the missing columns will be filled by it (as in the case of auto_increment primary keys).

If the resultset itself stores an assignment for the primary key, like in the case of:

my $restricted_rs = $user_rs->search( { id => 1 } );

you need to inform recursive_update about the additional predicate with the fixed_fields attribute:

For a many_to_many (pseudo) relation you can supply a list of primary keys from the other table and it will link the record at hand to those and only those records identified by them. This is convenient for handling web forms with check boxes (or a select field with multiple choice) that lets you update such (pseudo) relations.

If you pass additional data to recursive_update which doesn't match a column name, column accessor, relationship or many-to-many helper accessor, it will throw a warning by default. To disable this behaviour you can set the unknown_params_ok attribute to a true value.

If a many-to-many accessor key is included in the data structure with a value of undef or an empty array, all existing related rows are unlinked.

When the array contains elements they are updated if they exist, created when not and deleted if not included.

RecursiveUpdate defaults to calling 'set_$rel' to update many-to-many relationships. See "many_to_many" in DBIx::Class::Relationship for details. set_$rel effectively removes and re-adds all relationship data, even if the set of related items did not change at all.

If DBIx::Class::IntrospectableM2M is in use, RecursiveUpdate will look up the corresponding has_many relationship and use this to recursively update the many-to-many relationship.

While both mechanisms have the same final result, deleting and re-adding all relationship data can have unwanted consequences if triggers or method modifiers are defined or logging modules like DBIx::Class::AuditLog are in use.

The traditional "set_$rel" behaviour can be forced by passing "m2m_force_set_rel => 1" to recursive_update.