[Dbix-class] Adding stuff via many-to-many relationships

Robert Norris rob at cataclysm.cx
Wed Jun 14 07:48:21 CEST 2006


Part of my database is a list of users, and a list of groups. Each group
can have multiple user, and each user can be in multiple groups. I've
set up a fairly standard many-to-many relationship (ie two tables with a
link table). Schema proper is here: http://pastebin.com/707955.

Anyway, I'm wondering what the best practice is for adding things to the
database via many-to-many relationships. Consider the case of creating a
new group, and putting a new user in it. This obviously works:

    my $g = $s->resultset("Group")
              ->create({name => "caped crusaders"});
    $g->update;

    my $u = $s->resultset("User")
              ->create({name => "superman"});
    $u->update;

    $s->resultset("GroupUser")
      ->create({f_group => $g->groupid, f_user => $u->userid});

That seems rather clunky though. Why should I have to care about the
contents of the link table when DBIC already knows how these are linked
together.

I tried this to add the user:

    my $u = $g->users->create({name => "superman"});

But I get this:

    DBIx::Class::ResultSet::create(): No such column f_group on Emma::Schema::User

Did I screw my schema? They do look right to me, but the docs are a
little thin on this.

What is the "correct" way to do add stuff while also updating the link
table?

Rob.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.rawmode.org/pipermail/dbix-class/attachments/20060614/8cb45ccc/attachment.pgp 


More information about the Dbix-class mailing list