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

Alexander Hartmaier alexander.hartmaier at t-systems.at
Fri Apr 20 13:33:25 GMT 2012


Why has the Product table both an id and a productId column?
Aside from that it looks ok to me.
You might want to only specify the foreign column name in your
belongs_to rel to make the definition shorter and less error prone.

Best regards, Alex


Am 2012-04-20 15:02, schrieb Dmitry Bigunyak:
> 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?
>


*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
T-Systems Austria GesmbH Rennweg 97-99, 1030 Wien
Handelsgericht Wien, FN 79340b
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
Notice: This e-mail contains information that is confidential and may be privileged.
If you are not the intended recipient, please notify the sender and then
delete this e-mail immediately.
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*



More information about the DBIx-Class mailing list