[CDBI] relationships with mcpk's

Mark Hedges hedges at ucsd.edu
Wed Mar 15 01:07:39 GMT 2006


Hi all.  I'm a little new to Class::DBI but I understand 
relational databases okay and I'm trying to make a CDBI model 
for a new generation of our existing schema.

It seems like this should be easy but I can't find the answer in 
any docs or the wiki or list archives.

How do I define relationships from one class to another that has
multiple-column primary keys in its table?

    CREATE TABLE foo (
        bar         VARCHAR(16),      
        biz         VARCHAR(16),      
        PRIMARY KEY ( bar, biz )
    );
    
    CREATE TABLE baz (
        baz_id      SERIAL,
    
        bar         VARCHAR(16),
        biz         VARCHAR(16),
    
        PRIMARY KEY ( baz_id ),
        INDEX ( bar, biz )
    );

    # ... code:

    package My::Loader;
    
    use strict;
    use warnings;
    use Perl6::Export::Attrs;
    use Class::DBI;
    use Class::DBI::Loader;
    
    sub load_class_dbi :Export( :MANDATORY ) {
    
        my $loader = Class::DBI::Loader->new( 
            namespace => 'My', 
            # correct args etc. 
        );
    
        warn join(q{,}, My::Foo->primary_columns),"\n";
        # warns "bar,biz".
        
        # I feel like I should be able to do this:
    
        My::Baz->has_a( foo => 'My::Foo' );
    
        # but of course that says 
        #  "Column foo does not exist at 
        #       /usr/local/share/perl/5.8.8/Class/DBI/Relationship/HasA.pm 
        #       line 11"
    }

Do I do something with a TEMP column?  With some concatenation of
the pk names?  (That would be a pain to reference in tables with
5-column PK's and verbose names, ugh.)

I want to be able to say $baz->foo and get the Right Thing.

Any ideas?  Thanks.

Mark





More information about the ClassDBI mailing list