Syllabus Lesson 63 of 141 · Data models that bend
Data models that bend

Many, not one

Why it matters "A customer has an address" is the classic one-to-one assumption that turns out to be one-to-many. A second change hides behind it: when an address changes, what happens to things that already used the old one?

A small shop is writing its first order system. Today's request: customers sign up with an address, orders ship to the customer. Support already gets emails asking to send gifts to a work address, invoices to a PO box, and "I moved last week, where did my parcel go?".

Cardinality: how many of one thing go with one of another. One customer, many addresses.

Your turn

Today's request. Write new_book(), add_customer(book, customer, name, address), address(book, customer), place_order(book, order, customer) and ship_to(book, order).

What is coming. Customers will have more than one address, and they move. Hidden change requests will call these, so write them now as well:

  • add_address(book, customer, kind, address): kinds are strings like "work" or "billing"; the sign-up address is "home". Adding a kind the customer already has replaces it (they moved);
  • address(book, customer, kind="home") and addresses(book, customer) (a dict of kind to address);
  • place_order(book, order, customer, kind="home"). An order ships to the address of that kind as it was when the order was placed; a later move must not redirect it.

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.