sql - User database design for marketplace-style app -
need best-practice wisdom i'm new database architecture. marketplace type app. main issue i'd advice on handling users.
what i'd achieve buyer , seller accounts share login/signup functionality , characteristics, seller accounts have ability sell, receive payments, etc. buyers can send requests, sellers can complete them. seller can buyer can do, additionally view , fulfill requests, , sell.
i'd go simple roles, except sellers have more complex relationships buyers in terms of products , payment info. sellers need able publicly listed, , have public profiles. i'm not sure having 1 big table both users types ideal this.
my current idea use polymorphic associations between base user table , seller table (potentially buyer specific table well):
user (buyer)
- name
- encrypted_password
- (other authentication fields)
- location
- occupation
- meta_id
- meta_type
seller
- name
- location
- occupation
- etc
requests (belongs buyer , seller)
- type
- description
- complete
- buyer_id
- seller_id
products (belongs seller)
- description
- category_id
- seller_id
as can see, big issue fact buyers , sellers have duplicate data. reason because when i'm displaying sellers don't want have perform multi-table queries, maybe that's not issue? option have user base table , buyer , seller tables, they'd still contain duplicate info.
open possibilities.. what's best way go?
you can represent kind of relationship using database supertypes , subtypes.
for example, split data model 2 groups: users , roles. role can either buyer or seller, , user can have 0 or more roles.
i create following logical entities represent role relationship:
supertype
- userrole (this name generic; suggest name better reflects role of both buyer , seller within application).
subtypes
- buyer
- seller
for physical design, suggest 1 of following designs:
- a single table contains columns supertype entity columns each of subtype entities. check constraints can used enforce not-null constraints subtype columns.
- one table supertype, separate table each subtype entity. columns common each subtype stored in supertype table, other columns stored in appropriate subtype table. type column added supertype table indicate type of entity. each subtype table includes foreign key relationship supertype table.
- hybrid approach combines aspects each of above designs.
access patterns
one factor consider when deciding how model subtype , supertype relationship whether queries need access columns both supertype , subtype tables. if of queries access columns supertype , subtype tables, single table may better design.
edit - suggest using first design, unless there compelling reason create separate tables subtypes. foreign keys include type column can used restrict relationships particular subtype.
mapping user role
to assign user role, can create many-to-many relationship between user table , supertype (userrole) table.
Comments
Post a Comment