Schema question for owner of an object
I have a user_id colunm in the user table. Next I have a brand_开发者_如何学Pythonid in the brand table. The usecase is each has their own page: The user has a profile and the brand has a brand page. (NOTE: Thgere are more relationships like this. A company has a company page, city has city page, etc). So when users upload photos I need to reference it to the owner. Owner can be brand, user, city, organization, etc.
For user's it is straightforward, photo_id belongs to user_id where user_id is a FK to user table. But to add the brand_id as the owner of the photo, do i require a seperate colunm or can it be done in the user_id colunm itself if i rename it as owner_id which will reference (userid or brand id)?
2 columns, hands down. (One of the two must of course always be null)
With a single column you have a number of issues:
- No DB-level referential constraints (= foreign key)
- Risk of ID clashes between brand and user table
- Confusing
You could create one mapping table for each, such as user_photos
and brand_photos
.
That way you have full referential integrity from user_photos
to user
and brand_photos
to brands
. This also have a possitive impact on performance for queries that filters on user/brand photos.
精彩评论