One of the most useful Rose features to me is the

    $foo->bar({ unique_column => $value });

notation which will create a new unique_column in the referenced table or
re-use one that's already there. It seems though that this doesn't work
correctly for many-to-many relationsships and I wonder why.

Here's my mysql database:

    CREATE TABLE foos (
      id INTEGER AUTO_INCREMENT PRIMARY KEY NOT NULL,
      string VARCHAR(32),
      UNIQUE(string)
    )Type=InnoDB;

    CREATE TABLE bars (
      id INTEGER AUTO_INCREMENT PRIMARY KEY NOT NULL,
      string VARCHAR(32),
      UNIQUE(string)
    )Type=InnoDB;

    CREATE TABLE foo_bar_map (
      id INTEGER AUTO_INCREMENT PRIMARY KEY NOT NULL,
      foo_id INTEGER,
      bar_id INTEGER,
      FOREIGN KEY(foo_id) REFERENCES foos(id),
      INDEX(foo_id),
      FOREIGN KEY(bar_id) REFERENCES bars(id),
      INDEX(bar_id)
    )Type=InnoDB;

and here's my code:

    use Rose::DB::Object::Loader;

    my $loader =
        Rose::DB::Object::Loader->new(
            db_dsn       => 'dbi:mysql:dbname=testdb',
            db_username  => 'root',
            db_options   => { AutoCommit => 1, RaiseError => 1 },
            class_prefix => 'TestDB');

    $loader->make_classes();

    my $foo = TestDB::Foo->new();
    $foo->string("I am Foo");
    $foo->load(speculative => 1);

    $foo->add_bars({ string => "I am Bar" });
    $foo->save();

The first time this is run, records for both "I am Foo" in foos and "I am Bar" 
in bars
are created:

    mysql> select * from foos;
    +----+----------+
    | id | string   |
    +----+----------+
    |  1 | I am Foo |
    +----+----------+

    mysql> select * from bars;
    +----+----------+
    | id | string   |
    +----+----------+
    |  1 | I am Bar |
    +----+----------+

and the relationship table contains the mapping:

    mysql> select * from foo_bar_map;
    +----+--------+--------+
    | id | foo_id | bar_id |
    +----+--------+--------+
    |  1 |      1 |      1 |
    +----+--------+--------+

But the second time around, I would expect it to reuse the existing mapping, 
which
works well for the foos and bars tables, but surprisingly, it adds a second 
identical
mapping record:

    mysql> select * from foo_bar_map;
    +----+--------+--------+
    | id | foo_id | bar_id |
    +----+--------+--------+
    |  1 |      1 |      1 |
    |  2 |      1 |      1 |
    +----+--------+--------+

Is there a way around that? I'm using Rose::DB::Object 0.758.

-- Mike

Mike Schilli
[EMAIL PROTECTED]

-------------------------------------------------------------------------
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