Hi;

I'm following example discussed in 
http://sourceforge.net/mailarchive/forum.php?thread_id=9327291&forum_id=46239

Here is the code:
(I'm using MySQL and Rose::DB::Object version 0.724)

create table foo (
        id    int unsigned not null AUTO_INCREMENT PRIMARY KEY,
        name  varchar(255) not null,
UNIQUE (name)
);

create table foo_parent (
        parent_id int unsigned not null REFERENCES foo(id),
        child_id  int unsigned not null REFERENCES foo(id),
PRIMARY KEY (parent_id, child_id)
);


### file Foo.pm
package Foo;
use strict;
use FooParent;
use base qw(My::DB::Object);

__PACKAGE__->meta->table('foo');
__PACKAGE__->meta->columns(qw(id name));
__PACKAGE__->meta->primary_key_columns('id');
__PACKAGE__->meta->add_unique_key('name');
__PACKAGE__->meta->relationships(
        parents  => { 
                type      => 'many to many',
                map_class => 'FooParent',
                map_from  => 'child',
                map_to    => 'parent'
        },

        children => {
                type      => 'many to many',
                map_class => 'FooParent',
                map_from  => 'parent',
                map_to    => 'child'
        }
);

__PACKAGE__->meta->initialize;

1;

### file FooParent.pm
package FooParent;
use strict;

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

__PACKAGE__->meta->table('foo_parent');
__PACKAGE__->meta->columns(qw(parent_id child_id));
__PACKAGE__->meta->primary_key_columns(qw(parent_id child_id));

__PACKAGE__->meta->foreign_keys(
        parent => { class => 'Foo', key_columns => { parent_id => 'id' } },
        child  => { class => 'Foo', key_columns => { child_id  => 'id' } },
);

__PACKAGE__->meta->initialize;

1;

### file test-foo.pl
#! /usr/bin/perl -w
use strict;
use Foo;

#local $Rose::DB::Object::Debug = 1;

my $f1 = Foo->new(
                  name => 'f1: ' . localtime(),
                  children =>
                  [
                   {name => 'c1: ' . localtime()},
                   {name => 'c2: ' . localtime()},
                  ]
                 );

$f1->save;

exit;


Here the output and MySQL query.log (reformatted to save space):

> ./test-foo.pl 
> ./test-foo.pl 
DBD::mysql::st execute failed: Column 'child_id' cannot be null at 
/usr/lib/perl5/site_perl/5.8.1/Rose/DB/Object.pm line 836.
DBD::mysql::db rollback failed: Warning:  Some non-transactional changed tables 
couldn't be rolled back at /usr/lib/perl5/site_perl/5.8.1/Rose/DB.pm line 855.
rollback() - Warning:  Some non-transactional changed tables couldn't be rolled 
back at /usr/lib/perl5/site_perl/5.8.1/Rose/DB/Object.pm line 529.
insert() - DBD::mysql::st execute failed: Column 'child_id' cannot be null at 
/usr/lib/perl5/site_perl/5.8.1/Rose/DB/Object.pm line 836.
 at /usr/lib/perl5/site_perl/5.8.1/Rose/DB/Object/MakeMethods/Generic.pm line 
3665
 at ./test-foo.pl line 16

                  
(1)INSERT INTO `foo` ( `id`, `name`) 
           VALUES ( NULL, 'f1: 1147897829')

(2)DELETE FROM `foo_parent` 
WHERE  `parent_id` = '26'

(3)SELECT `id`, `name` FROM `foo` 
WHERE name = 'c1: 1147897829'

(4)INSERT INTO `foo`( `id`, `name` ) 
           VALUES( NULL, 'c1: 1147897829')

(5)INSERT INTO `foo_parent`( `child_id`, `parent_id`) 
                 VALUES ( NULL, '26')
                           ^
             --  should be 27
Quit       

In other words, it remembers the parent_id=26 generated by query (1),
and misses child_id=27, generated by query (4).

I can solve the problem by introducing class Bar as Foo's twin brother,
pointing both to the same table "foo", and using Foo as a parent and
Bar as a child, but it seems unnatural.

On a minor note, 
local $Rose::DB::Object::Debug = 1
doesn't print query (2).

Please help.
Thank you.
-- 
Regards,
Michael


-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object

Reply via email to