[Dbix-class] has_one joining through another table

Steve Francia steve at takkle.com
Tue Jun 26 23:45:11 GMT 2007


Matt S Trout wrote:
> On Tue, Jun 26, 2007 at 03:05:06PM -0700, Marc Mims wrote:
>   =

>> * Steve Francia <steve at takkle.com> [070626 14:40]:
>>     =

>>> in User Schema:
>>> __PACKAGE__->has_many(user_pictures =3D>'Schema::UserPicture', 'fk_user=
_id');
>>> __PACKAGE__->many_to_many(pictures =3D> 'user_pictures', 'picture');
>>>
>>> in UserPicture Schema:
>>> __PACKAGE__->belongs_to(user =3D> 'Schema::User', 'fk_user_id');
>>> __PACKAGE__->belongs_to(picture =3D> 'Schema::Picture', 'fk_picture_id'=
);
>>>
>>> in Picture Schema:
>>> __PACKAGE__->has_many(user_pictures =3D>'Schema::UserPicture',
>>> 'fk_picture_id');
>>> __PACKAGE__->many_to_many(users =3D> 'user_pictures', 'user');
>>>       =

>> I would add a user foreign key to picture and drop the owner flag from
>> user_picture.  The the relationship is straight forward:
>>
>> In Picture Schema:
>> __PACKAGE__->belongs_to(owner =3D> 'Schema::User', 'owner_id');
>>     =

>
> Agreed. The schema as-is isn't sane since there's no way to enforce
> that only one user is the owner of a given picture.
>
> Unless you're planning to have multiple owners, which I didn't infer from
> what you said before.
>
>   =

Sadly, that is the way we had it, we decided to switch to using the =

owner flag to try to eliminate duplication of data in the database.

This does bring up an interesting question though. If there were =

multiple owners, but then that would just be a fairly typical =

many_to_many relationship, right?

Is there a way to do it given the current database layout? It would be =

good to know from a design standpoint, if such a thing were possible. I =

have a more complicated example that requires an answer to this simpler =

one. It is more or less the same exact scenario, except instead of =

owner, it is type. There are 3 types, which translate to three different =

relationships, two are a many_to_many, one is a has_one. I know the =

database wouldn't enforce that, but our application would.

-Steve

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20070626/9dd=
b6f54/attachment.htm


More information about the Dbix-class mailing list