Tom
Tom

Reputation: 2661

how to update one to many relationship in ddb

Let's assume a bank and address example:

class Address: 
    id: string
    street: string
    city: string

class BankBranch:
    id: string 
    name: string 
    employee_count: number
    address: object

Address is its own table because other services should be able to use instances of this Address table. For example each BankBranch has an address. Now when I store a BankBranch instance in DBB I'd imagine it will look like this:

{
    name: "BranchXYZ",
    id: "123456",
    employee_count: 5,
    address: {
        street: "123 main st",
        city: "maincity",
        id: "456789"
    }
} 

and the address instance looks like this:

{
    street: "123 main st",
    city: "maincity",
    id: "456789",
}

What's the best procedure to keep the data consistent? If I update the address instance, do I issue another call to the BankBranch and update it there as well? So one update operation is actually 2 database operations?

Or instead of saving the entire address object I just save the id (like a foreign key) and then when I go I get request to the BankBranch instance I issue another request to the address issue? That sounds a bit expensive.

Upvotes: 0

Views: 55

Answers (1)

hunterhacker
hunterhacker

Reputation: 7122

You’re asking if you should denormalize or not. That depends on many factors.

First, are you doing this at a scale where it actually matters? You say it’s expensive. At highest price EC lookups are about 12.5c per million lookups. How many secondary lookups would you really be doing?

Meanwhile you haven’t explained in depth why the address has to be in another table. Seems like it’s an intrinsic part of the branch. You’re really going to have other services referring to the address by some random ID number unrelated to the branch? Can’t they refer to it by branch ID and pull out the address?

Upvotes: 1

Related Questions