Best way to model Customer <--> Address

后端 未结 12 1615
被撕碎了的回忆
被撕碎了的回忆 2020-11-30 02:22

Every Customer has a physical address and an optional mailing address. What is your preferred way to model this?

Option 1. Customer has fo

相关标签:
12条回答
  • 2020-11-30 02:27

    I tend towards first approach for all the usual reasons of normalisation. This approach also makes it easier to perform data cleansing on mailing details.

    If you are possibly going to allow multiple addresses (mail, residential, etc) or wish to be able to use effective dates, consider this approach

       Customer   (id, phys_address_id)
       Cust_address_type (cust_id, mail_address_id, address_type, start_date, end_date)
       Address    (id, street, city, etc.)
    
    0 讨论(0)
  • 2020-11-30 02:29

    When answering those kinds of questions I like to use the classifications of DDD. If it's a Entity it should have a separate ID, if it's a value object it should not.

    0 讨论(0)
  • 2020-11-30 02:30

    Good thread. I have spent a while contemplating the most suitable schema and I have concluded that quentin-starin's solution is the best except I have added start_date and end_date fields to what would be his PersonAddress table. I have also decided to add notes, active and deleted.

    deleted is for soft delete functionality as I think I do not want to lose trace of previous addresses simply by deleting the record from the junction table. I think that is quite wise and something others may want to consider. If not done this way, it could be left to revision of paper or electronic documents to try to trace address information (something best avoided).

    notes I think of being something of a requirement but that might just be preference. I've spent time in backfill exercises verifying addresses in databases and some addresses can be very vague (such as rural addresses) that I think it is very useful to at least allow notes about that address to be held in the record address.

    One thing i would like to hear opinions on is the unique indexing of the address table (again, referring to the table of the same name in quentin-starin's example. Do you think it should be unique index should be enforced (as a compound index presumably across all not-null/required fields)? This would seem sensible but it might still be hard to stop duplicate data regardless as postal/zip codes are not always unique to a single property. Even if the country, province and city fields are populated from reference data (which they are in my model), spelling differences in the address lines may not match up. The only way to best avoid this might be to run one or a number of DB queries from the incoming form fields to see if a possible duplicate has been found. Another safety measure would be give the user the option of selecting from address in the database already linked to that person and use that to auto-populate. I think this might be a case where you can only be sensible and take precautions to stop duplication but just accept it can (and probably will) happen sooner or later.

    The other very important aspect of this for me is future editing of the address table records. Lets say you have 2 people both listed at: -

    11 Whatever Street Whatever City Z1P C0D3

    Should it not be considered dangerous to allow the same address table record to be assigned to different entities (person, company)? Then let's say the user realises one of these people lives at 111 Whatever Street and there is a typo. If you change that address, it will change it for both of the entities. I would like to avoid that. My suggestion would be to have the model in the MVC (in my case, PHP Yii2) look for existing address records when a new address is being created known to be related to that customer (SELECT * FROM address INNER JOIN personaddress ON personaddress.address_id = address.id WHERE personaddress.person_id = {current person being edited ID}) and provide the user the option of using that record instead (as was essentially suggested above).

    I feel linking the same address to multiple different entities is just asking for trouble as it might be a case of refusing later editing of the address record (impractical) or risking that the future editing of the record may corrupt data related to other entities outside of the one who's address record is being edited.

    I would love to hear people's thoughts.

    0 讨论(0)
  • 2020-11-30 02:32

    The second option would probably be the way I would go. And on the off-chance it would let users add additional address' (If you wanted to let them do that), that they could switch between at will for shipping and such.

    0 讨论(0)
  • 2020-11-30 02:39

    We are moving forward with a model like this:

    Person (id, given_name, family_name, title, suffix, birth_date)
    Address (id, culture_id, line1, line2, city, state, zipCode, province, postalCode)
    AddressType (id, descriptiveName)
    PersonAddress (person_id, address_id, addressType_id, activeDates)
    

    Most may consider this excessive. However, an undeniable common theme amongst the apps we develop is that they will have some of these fundamental entities - People, Organizations, Addresses, Phone Numbers, etc.. - and they all want to combine them in different ways. So, we're building in some generalization up-front that we are 100% certain we have use cases for.

    The Address table will follow a table-per-hierarchy inheritance scheme to differentiate addresses based on culture; so a United States address will have a state and zip field, but Canadian addresses will have a province and postal code.

    We use a separate connecting table to "give" a person an address. This keeps our other entities - Person & Address - free from ties to other entities when our experience is this tends to complicate matters down the road. It also makes it far simpler to connect Address entities to many other types of entities (People, Organizations, etc.) and with different contextual information associated with the link (like activeDates in my example).

    0 讨论(0)
  • 2020-11-30 02:39

    I would go with option 1. If you want to, you could even modify it a little bit to keep an address history:

    Customer   (id, phys_address_id, mail_address_id)
    Address    (id, customer_id, start_dt, end_dt, street, city, etc.)
    

    If the address changes, just end date the current address and add a new record in the Address table. The phys_address_id and mail_address_id always point to the current address.

    That way you can keep a history of addresses, you could have multiple mailing addresses stored in the database (with the default in mail_address_id), and if the physical address and mailing address are identical you'll just point phys_address_id and mail_address_id at the same record.

    0 讨论(0)
提交回复
热议问题