google app engine - Complicated, costly many-to-many entity references -
i have 4 main kinds: account, company, service , address. address entities shared between company , service entities.
- account: user account (email, password)
- company: business provide services (ancestor: account)
- service: service rendered company (ancestor: company)
- address: address (group of fields: street, city, country) of company or service (ancestor: account)
the challenge: company , service entities may have different addresses; after all, company's address not services acquired. services may have many addresses, since company may set different franchises/outlets services may acquired.
i model data in such way addresses can referenced either company or service entities, or both. have tried these 2 approaches:
let's assume address model:
class address(ndb.model): street = ndb.stringproperty(required=true) city = ndb.stringproperty(required=true) country = ndb.stringproperty(required=true)
approach 1: store list of address keys inside service or company
class service(ndb.model): title = ndb.stringproperty(required=true) addresses = ndb.keyproperty(repeated=true) class company(ndb.model): name = ndb.stringproperty(required=true) addresses = ndb.keyproperty(repeated=true)
problem: each page view of service or company, need perform additional queries fetch respective addresses. blows big expensive problem our entities grow in number.
approach 2: create addressmapping entity forms relationship between 2 entities:
class service(ndb.model): title = ndb.stringproperty(required=true) addresses = ndb.keyproperty(repeated=true) class addressmapping(ndb.model): entity = ndb.stringproperty(required=true) # service or company address = ndb.keyproperty(repeated=true)
problem: if service disabled/deleted/modified, need delete/modify accompanying addressmapping entities, or else orphaned. additional queries still required when viewing pages. seems expensive.
these 2 approaches i've come with; both seem bad. ideas on how may improve this?
if store keys of addresses in company , service models, not need "additional queries fetch them" - can get
address entities need. fast , cheap.
Comments
Post a Comment