[Dbix-class] Relationship problems
Jess Robinson
castaway at desert-island.demon.co.uk
Sat Nov 11 00:22:35 GMT 2006
On Fri, 10 Nov 2006, Josef Karthauser wrote:
> Hey there,
>
> I wonder, would someone be able to help me with a multi field key that
> I'm having trouble with?
Yup
> I've got three tables:
>
> package Contact;
> use base qw/DBIx::Class/;
>
> __PACKAGE__->load_components(qw/ PK::Auto Core /);
> __PACKAGE__->table('contact');
> __PACKAGE__->add_columns(qw/
> contactid, owner, first_name, last_name, org_name/);
> __PACKAGE__->set_primary_key(qw/contactid owner/);
> __PACKAGE__->has_many(domains => 'Domain',
> { 'registrant' => 'contactid', 'owner' => 'owner' } );
Should be:
__PACKAGE__->has_many(domains => 'Domain',
{ 'foreign.registrant' => 'self.contactid',
'foreign.owner' => 'self.owner' } );
> __PACKAGE__->belongs_to(owner => 'Owner');
>
> package Domain;
> use base qw/DBIx::Class/;
>
> __PACKAGE__->load_components(qw/ PK::Auto Core /);
> __PACKAGE__->table('domain');
> __PACKAGE__->add_columns(qw/domainid domainnam owner registrant/);
> __PACKAGE__->set_primary_key('domainid');
> __PACKAGE__->belongs_to(owner => 'Owner');
> __PACKAGE__->belongs_to(registrant => 'Contact',
> { owner => 'owner', contactid => 'registrant' });
Should be:
__PACKAGE__->belongs_to(registrant => 'Contact',
{ 'foreign.owner' => 'self.owner',
'foreign.contactid' => 'self.registrant' });
> package Tao::DBIx::Owner;
> use base qw/DBIx::Class/;
>
> __PACKAGE__->load_components(qw/ PK::Auto Core /);
> __PACKAGE__->table('owner');
> __PACKAGE__->add_columns(qw/ownerid contact/);
> __PACKAGE__->set_primary_key('ownerid');
> __PACKAGE__->belongs_to(contact => 'Tao::DBIx::Contact',
> { ownerid => 'owner', contact => 'contactid' });
You can figure this one out..
>
> I've removed the columns which are not relevant to this query, so the
> database looks a bit sparse. Notice that the contact table has a two
> column primary key.
>
> The problem I have is with the has_many relationship in the contact
> table. Each contact can have many domains, and I want to read them.
>
> $contact = 1234;
> print $contact->first_name, "\n";
> my @domains = $contact->domains;
> exit;
>
> However I get a relationship error,
>
> % ./test3.pl
> Kieren
> DBIx::Class::Relationship::Accessor::__ANON__(): Invalid rel cond key
> owner at ./test3.pl line 3
>
Certainly, though I prefer fishing rods, try:
DBIx::Class::Relationship::Base for docs on foreign/self. I guess I should
add more examples of these into ::Relationship.
Jess
More information about the Dbix-class
mailing list