Re: many2many

[prev] [thread] [next] [Date index for 2005/04/11]

From: William Ross
Subject: Re: many2many
Date: 18:58 on 11 Apr 2005
On 11 Apr 2005, at 17:46, Barry Dancis wrote:

> I am trying to implement a many to many relationship with
> =A0
> #=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D
> {package Sites;
> =A0 Sites->has_many (siteenzymes=A0 =3D> ['EnzymeSites' =3D> =
'enzid']);
>
> #=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D
> {package Enzymes;
> =A0 Enzymes->has_many (enzsites =3D> ['EnzymeSites' =3D> 'siteid']);
>
> #=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D
> {package EnzymeSites;
> =A0 EnzymeSites->columns(Primary =3D> qw/ siteid enzid /);
> =A0 EnzymeSites->has_a (enzid=A0 =3D> 'Enzymes');
> =A0 EnzymeSites->has_a (siteid=A0 =3D> 'Sites');
> =A0
> but when I execute:
> =A0
> =A0 my $enzyme =3D Enzymes->retrieve(enzyme =3D> 'TstI');
> =A0=A0my @sites =3D $enzyme->enzsites;
> I get the error:
> =A0
> =A0=A0=A0 enzymes is not a column of EnzymeSites

The foreign class column name in a has_many relationship defaults to=20
the moniker of the calling class, on the reasonable assumption that=20
when a cd has many tracks, the cd of the track will be recorded in a=20
column called 'cd'. This is in the docs.

If i remember rightly, the moniker of a class defaults to the name of=20
its data table if no other moniker is declared. I don't think this is=20
in the docs.

Your Enzymes class is declaring a has_many relationship with=20
EnzymeSites. The extra [] clause just tells that relationship object to=20=

return a column of the EnzymeSites object rather than returning the=20
whole object.

The data table for your Enzyme class is called 'enzymes'. By default=20
the has_many relationship will get that as the moniker and look for a=20
column with that name in the EnzymeSites class, but the real name of=20
the column is 'enz_id'. To avoid this trouble, you just have to specify=20=

the column name. To avoid future trouble, I'd specify the class moniker=20=

too:

	Enzymes->moniker('enzyme');
	Enzymes->has_many(enzsites =3D> ['EnzymeSites' =3D> 'siteid'], =
'enzid');
	Sites->has_many (siteenzymes=A0 =3D> ['EnzymeSites' =3D> =
'enzid'], 'siteid');

Personally I also avoid the 'id' suffix on column names: it describes=20
the contents of the column well enough, but becomes confusing when the=20=

column is inflated. In your case, it leads to statements like:

	my $site_id =3D $enzyme->site_id->siteid;

But that's very much a question of taste, and there are plenty here who=20=

would disagree.

best

Will

ps. Tony, didn't the moniker used to de-pluralise itself? Or did I=20
imagine that?

many2many
Barry Dancis 16:46 on 11 Apr 2005

Re: many2many
William Ross 18:58 on 11 Apr 2005

Re: many2many
Tony Bowden 19:17 on 11 Apr 2005

Re: many2many
William Ross 19:38 on 11 Apr 2005

Re: many2many
Barry Dancis 14:43 on 12 Apr 2005

Re: many2many
William Ross 17:23 on 12 Apr 2005

Re: many2many
Barry Dancis 15:43 on 12 Apr 2005

Generated at 12:49 on 16 Apr 2005 by mariachi v0.52