Hi-

First time poster. :)

I have a simple mysql InnoDB database setup with two tables, Parent and
Child.  Child contains a foreign key into Parent.  It's a pretty trivial
setup, but I'm new to Rose::DB::Object and I'd like to be able to insert a
new Product and have the Child inserted automatically.

My Rose::DB::Object base class looks like :

package My::DB::Object;

use strict;
use warnings;
use lib '/home/user/larry/scratch/rose_test';
use My::DB;
use base qw(Rose::DB::Object);

sub init_db { My::DB->new() }

1;

perl_class_definition() ouput for the Parent table looks like this :

package My::DB::Parent;

use strict;

use base qw(My::DB::Object);

__PACKAGE__->meta->setup
(
 table   => 'parent',

 columns =>
 [
   parent_id => { type => 'integer', not_null => 1 },
   notes     => { type => 'text', default => '', length => 65535, not_null
=> 1 },
 ],

 primary_key_columns => [ 'parent_id' ],

 relationships =>
 [
   child =>
   {
     class      => 'My::DB::Child',
     column_map => { parent_id => 'parent_id' },
     type       => 'one to many',
   },
 ],
);
__PACKAGE__->meta->make_manager_class( 'parent' );
1;

The perl_class_definition() output for the Child table looks like this :

package My::DB::Child;

use strict;

use base qw(My::DB::Object);

__PACKAGE__->meta->setup
(
 table   => 'child',

 columns =>
 [
   child_id  => { type => 'integer', not_null => 1 },
   parent_id => { type => 'integer', default => '', not_null => 1 },
   notes     => { type => 'text', default => '', length => 65535, not_null
=> 1 },
 ],

 primary_key_columns => [ 'child_id' ],

 foreign_keys =>
 [
   parent =>
   {
     class       => 'My::DB::Parent',
     key_columns => { parent_id => 'parent_id' },
   },
 ],
);

__PACKAGE__->meta->make_manager_class( 'child' );
1;

I then ran a small driver script with the following :

...
...
my $p = My::DB::Parent->new(
   notes => "These are some parent notes")->save;

$p->child(notes => "These are some child notes");
$p->save;

The way I understood the above to work was that it would insert a new Parent
row and then auto-insert ONE (1) Child row with the child.parent_id foreign
key pointed back to parent.parent_Id.  But there are two Child rows being
inserted, and neither have the child.notes field populated  :

Parent Table:
parent_id notes   00000000001 These are some parent notes
Child Table:
child_id parent_id notes   00000000001 00000000001    00000000002
00000000001
I would have thought the Child table should look like this :
child_id    parent_id        notes
00000000001 00000000001      These are some child notes

Any ideas why this happened?  Do I not have my tables setup correctly?

Any help would be great!

Thanks,
Chris
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object

Reply via email to