سؤال

How would i go about creating a relationship for photo albums that belong to either a user, event, group, network, page (I want to support x number of possible owners)? If possible even support multiple owners lateron.

One way is to have columns in the album table for these with one of them have a FK ID and rest be null. But problem is as i add more owner types i have to keep altering the table to add more columns

The other way is to have separate photo tables for each of these owner types.

None of these methods scale well. Any other methods to own photo albums for different owner types from one table that will scale (meaning have less joins and be easy to query as read performance is my #1 requirement?

Platform is MySQL/PHP.

هل كانت مفيدة؟

المحلول

Think of your problem in a Conceptual Data Model instead of purely physical. If you do OO work, that's fine too. CDM

The semicircle is an inheritance operator. So Event, User, Network, Page all inherit from Owner.

When you generate this to a Physical model you'll use these settings.

alt text

This way you get one table of everything which can own a photo and all of the common attributes. Then each child table will inherit the PI from the parent, and have attributes specific to it. The FK of the photo table will go to the Owner table.

Your future goal of many owners for a photo changes from a Owner ID as FK on the Photo table to a many:to:many mapping table between owner and photo. Really easy if you use these constructs.

نصائح أخرى

Create a table with three columns, album FK, owner FK, and owner type with owner type being a enumerable type for user, even, group, etc.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top