Matt S Trout wrote:
On Tue, Jun 26, 2007 at 03:05:06PM -0700, Marc Mims wrote:
* Steve Francia <[EMAIL PROTECTED]> [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.

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

_______________________________________________
List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
Wiki: http://dbix-class.shadowcatsystems.co.uk/
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
Searchable Archive: http://www.mail-archive.com/[email protected]/

Reply via email to