Subject: | Resultset create method doesn't create related rows |
Hi.
According to the Resultset documentation on the create method
(http://search.cpan.org/~arodland/DBIx-Class/lib/DBIx/Class/ResultSet.pm#create) I'm
expecting it can create related rows in the related relationships. But on my attempt to
insert related rows with the main one I get the error:
"DBIx::Class::ResultSet::create(): Unable to determine relationship 'images' direction
from 'Product', possibly due to a missing reverse-relationship on 'images' to
'Product'.".
Please, take a look at my two tables schema.
#=========================================
package Related::Schema::Result::Product;
use strict;
use warnings;
use base 'DBIx::Class';
__PACKAGE__->load_components("Core");
__PACKAGE__->table("Product");
__PACKAGE__->add_columns(
'id' => {
data_type => 'int',
is_auto_increment => 1,
is_nullable => 0,
},
'itemId' => {
data_type => 'varchar',
size => 255,
is_nullable => 0,
},
'data' => {
data_type => 'blob',
is_nullable => 0,
},
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->has_many(
'images' => 'Related::Schema::Result::ProductImage',
{ 'foreign.productId' => 'self.id' },
{ cascade_delete => 1, join_type => 'left' }
);
#=========================================
package Related::Schema::Result::ProductImage;
use strict;
use warnings;
use base 'DBIx::Class';
__PACKAGE__->load_components("Core");
__PACKAGE__->table("ProductImage");
__PACKAGE__->add_columns(
'id' => {
data_type => 'int',
is_auto_increment => 1,
is_nullable => 0,
},
'productId' => {
data_type => 'int',
is_nullable => 0,
},
'url' => {
data_type => 'varchar',
size => 255,
is_nullable => 0,
},
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->belongs_to(
'product' => 'Related::Schema::Result::Product',
{ 'foreign.id' => 'self.productId' },
{ is_foreign_key_constraint => 0, join_type => 'inner' }
);
#=========================================
Below how I'm trying to insert the data
my $product_row = $schema->resultset("Product")->create({
itemId => 'A1',
data => "SERIALIZED PRODUCT DATA",
images => [ { url => 'http://url1' }, { url => 'http://url2' } ],
});
As you can see I have relationships in both directions: has_many and corresponding
belongs_to. So even if I misunderstood the documentation and create method doesn't do
the trick the error description doesn't explain the problem.