help with has_a and has_many relationships?

[prev] [thread] [next] [Date index for 2004/05/19]

From: Caroline Johnston
Subject: help with has_a and has_many relationships?
Date: 11:49 on 19 May 2004
Hi,

I've just started using cdbi and I can't quite get the hang of setting up
relationships. I think I'm probably missing something obvious here, but I
can't figure out what's going on. Any help would be appreciated!

I have a class Sessions_Metadata which contains foreign keys to classes 
Users and Sessions (see below). I have a test script which creates an 
instance of Sessions_Metadata and attempts the following tests:

[1] isa_ok($sessions_metadata->username, 
"EP::Common::DBI::Users");
[2] isa_ok($sessions_metadata->session_id,
"EP::Common::DBI::Sessions");

If I only run test [2], it works fine and I get:

ok 1 - The object isa EP::Common::DBI::Sessions

If I run both tests, or I run only test[1] I get an error:

Can't bind a reference (EP::Common::DBI::Sessions=HASH(0x85d1d70)) at 
/usr/local/lib/perl5/site_perl/5.8.1/DBIx/ContextualFetch.pm line 51, 
<FILE> line 47.
1..5

So, I thought the problem was with test[1] and the Users class (even
though the error refers to Sessions), but if I comment out the
has_a(session_id=>'EP::Common::DBI::Sessions')  line in the
Sessions_Metadata class, and run test[1] I get:

ok 1 - The object isa EP::Common::DBI::Users

So, if I have both has_a definitions, the Sessions class works, but the 
Users class doesn't. If I only have the has_a defined for Users then the 
Users class works. What's going on?

Someone on perlmonks suggested the patch at 
http://groups.kasei.com/mail/arc/cdbi-talk/2004-04/msg00200.html, but it 
doesn't seem to help. Also, someone else suggested just not using Foreign 
keys as Primary keys but unfortunately changing the underlying DB 
isn't an option. Any other ideas...?


Many thanks

Cass





###### Users ####### 
package EP::Common::DBI::Users; 
use strict; 
use warnings; 
use base qw( EP::Common::DBI ); 
__PACKAGE__->table('users');
__PACKAGE__->columns(Primary => qw/username/); 
__PACKAGE__->columns(All => qw/password salt session_only first_name 
last_name institute department address telephone email/);


###### Sessions ########
package EP::Common::DBI::Sessions;
use strict;
use warnings;
use base qw( EP::Common::DBI );
__PACKAGE__->table('sessions');
__PACKAGE__->columns(Primary => qw/id/);
__PACKAGE__->columns(All => qw/a_session/);


######### Sessions_Metadata #################
package EP::Common::DBI::Sessions_Metadata;
use strict;
use warnings;
use base qw( EP::Common::DBI );
#setup table fields
__PACKAGE__->table('sessions_metadata');
__PACKAGE__->columns(Primary => qw/session_id/);
__PACKAGE__->columns(Others => qw/create_date username expire query_id/);
#relations
__PACKAGE__->has_a(username=>'EP::Common::DBI::Users');
__PACKAGE__->has_a(session_id=>'EP::Common::DBI::Sessions');


help with has_a and has_many relationships?
Caroline Johnston 11:49 on 19 May 2004

Re: help with has_a and has_many relationships?
Tony Bowden 11:54 on 19 May 2004

Re: help with has_a and has_many relationships?
Caroline Johnston 13:28 on 19 May 2004

Generated at 11:34 on 01 Dec 2004 by mariachi v0.52