in reply to many to many with Class::DBI

replace

__PACKAGE__->columns(All => qw(user_id role_id));
with
__PACKAGE__->columns(Primary => qw(user_id role_id));
and your code should work (it does here ;-).

an error likely to be overlooked! I spotted it reading the Class::DBI documentation; I don't know what the difference in the generated code is, and frankly: I don't care ;-) It's a cool module!

Edit:tested with perl 5.8.3 and Class::DBI 0.95. without the mentioned change I didn't get your error but wrong results (all three roles as 'typist') instead.

regards,
tomte


Hlade's Law:

If you have a difficult task, give it to a lazy person --
they will find an easier way to do it.

Replies are listed 'Best First'.
Re: Re: many to many with Class::DBI
by hardburn (Abbot) on Feb 12, 2004 at 14:06 UTC

    I don't know what the difference in the generated code is, and frankly: I don't care ;-)

    You should care. That code makes both user_id and role_id a primary key, which means you now have to specify both when you make a call to retrieve. Further, primary keys have a specific definition under relational database theory, and you can't go making just anything a primary key or Bad Things will happen.

    ----
    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

      You should care....

      No, I shouldn't :-), I could!
      because, at least in the code I tested, I created the database with SQL myself, knowing about primaray keys, foreign keys etc.

      and after that I used a Perl-Module, namely Class::DBI, to access this DB, according to it's documentation; the documentation stated clearly that many-to-many relations aren't supported directly, but how to circumvent this fact using the mappings available via Class::DBI

      So I should care about my data-definition, you are right with that, but shouldn't care about the internals of a module I use, if this works as stated in the documentation, and nothing else was my intention to express in my first post.

      regards,
      tomte


      Hlade's Law:

      If you have a difficult task, give it to a lazy person --
      they will find an easier way to do it.

        The code you tested works because the problem was circumvented without actually solving it. Try retrieving one key from that table instead of accessing it through relationships and the problem will become apparent. With specifying extra params with Primary, you no longer have a Class::DBI object that matches up with your data definition. The definition says that a primary key for this table is made up of a single column, so that's what Class::DBI needs to think, too.

        This doesn't have anything to do with how Class::DBI is handling it internally, as all this is plainly documented. I'm not sure what the real solution is (I don't have time to make a test database and run some code against it), but I'm sure this isn't the correct solution.

        ----
        : () { :|:& };:

        Note: All code is untested, unless otherwise stated

      So how fix without Primar?
Re: Re: many to many with Class::DBI
by jdtoronto (Prior) on Feb 12, 2004 at 15:02 UTC
    I updated to 0.95 this morning and the error went away. Your suggested change got me the data I wanted, thank you. Now I can go back and fix my reall application and make some notes in my files about CDBI.

    ...john