[Dbix-class] Resultset doesn't create related rows

Dmitry Bigunyak icestar at inbox.ru
Fri Apr 20 13:02:15 GMT 2012


Hi everyone!

 From the ResultSet documentation on the create method 
(http://search.cpan.org/~arodland/DBIx-Class/lib/DBIx/Class/ResultSet.pm#create) 
I understood that it's able to create related rows in related relationships.
But on my attempt to create the main row with some related 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'."

My Product table is described below:
__PACKAGE__->load_components("Core");
__PACKAGE__->table("Product");
__PACKAGE__->add_columns(
     'id' => {
         data_type => 'int',
         is_auto_increment => 1,
         is_nullable => 0,
     },
     'productId' => {
         data_type => 'varchar',
         size => 255,
         is_nullable => 0,
     },
     'data' => {
         data_type => 'blob',
         is_nullable => 0,
     },
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->has_many(
     'images' => 'My::Result::ProductImage',
     { 'foreign.productId' => 'self.id' },
     { cascade_delete => 1, join_type => 'left' }
);

Here is my ProductImage table:
__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' => 'My::Result::Product',
     { 'foreign.id' => 'self.productId' },
     { is_foreign_key_constraint => 0, join_type => 'inner' }
);

I'm trying to perform the insertion like that:
my $product_row = $schema->resultset("Product")->create({
     productId => $product->{id},
     data      => $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.
Can you help me to find the problem what I'm dong wrong?
Maybe I misunderstood the documentation and create doesn't do the trick?

-- 
Dmitry Bigunyak
e-mail: icestar at inbox.ru




More information about the DBIx-Class mailing list