[Dbix-class] has_one joining through another table

Matt S Trout dbix-class at trout.me.uk
Tue Jun 26 23:31:44 GMT 2007


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 =>'Schema::UserPicture', 'fk_user_id');
> > __PACKAGE__->many_to_many(pictures => 'user_pictures', 'picture');
> > 
> > in UserPicture Schema:
> > __PACKAGE__->belongs_to(user => 'Schema::User', 'fk_user_id');
> > __PACKAGE__->belongs_to(picture => 'Schema::Picture', 'fk_picture_id');
> > 
> > in Picture Schema:
> > __PACKAGE__->has_many(user_pictures =>'Schema::UserPicture',
> > 'fk_picture_id');
> > __PACKAGE__->many_to_many(users => '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 => '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.

-- 
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director    Want a managed development or deployment platform?
 Shadowcat Systems Ltd.  Contact mst (at) shadowcatsystems.co.uk for a quote
http://chainsawblues.vox.com/             http://www.shadowcatsystems.co.uk/ 



More information about the Dbix-class mailing list