Hone logo
Hone
Problems

Implementing Cascading Deletes in a Relational Data Structure

Cascading deletes are a crucial feature in relational databases, ensuring data integrity when relationships exist between tables. This challenge asks you to implement a simplified version of cascading deletes in Python, simulating the behavior of deleting a record and automatically deleting related records in other tables based on defined relationships. This is useful for maintaining consistency in data structures where dependencies exist.

Problem Description

You are tasked with creating a Python function that simulates cascading deletes within a simplified relational data structure represented as dictionaries. The data structure consists of two tables: customers and orders. Each table is a dictionary where keys are record IDs and values are dictionaries containing the record's attributes. The orders table has a customer_id attribute that links it to the customers table.

The function should take two arguments:

  1. data: A dictionary representing the combined data of the customers and orders tables.
  2. record_id: The ID of the record to be deleted from the customers table.

The function must perform the following actions:

  1. Delete the specified record from the customers table.
  2. Identify all orders in the orders table that are associated with the deleted customer (i.e., where customer_id matches the deleted customer's ID).
  3. Delete those associated orders from the orders table.
  4. Return the modified data dictionary after the cascading delete operation.

Key Requirements:

  • The function must handle cases where the record_id does not exist in the customers table gracefully (return the original data without modification).
  • The function must correctly identify and delete all related orders.
  • The function should not modify the data structure if the record_id is not found in the customers table.

Expected Behavior:

The function should modify the data dictionary in place, removing the specified customer and all associated orders. The returned dictionary should reflect these changes.

Edge Cases to Consider:

  • record_id not found in customers.
  • No orders associated with the customer.
  • Empty data dictionary.
  • Multiple orders associated with the same customer.

Examples

Example 1:

Input:
data = {
    'customers': {
        '1': {'name': 'Alice', 'city': 'New York'},
        '2': {'name': 'Bob', 'city': 'London'}
    },
    'orders': {
        '101': {'customer_id': '1', 'item': 'Book'},
        '102': {'customer_id': '2', 'item': 'Laptop'},
        '103': {'customer_id': '1', 'item': 'Pen'}
    }
}
record_id = '1'
Output:
{
    'customers': {
        '2': {'name': 'Bob', 'city': 'London'}
    },
    'orders': {
        '102': {'customer_id': '2', 'item': 'Laptop'}
    }
}
Explanation: Customer '1' (Alice) and orders '101' and '103' are deleted.

Example 2:

Input:
data = {
    'customers': {
        '1': {'name': 'Alice', 'city': 'New York'}
    },
    'orders': {
        '101': {'customer_id': '2', 'item': 'Book'}
    }
}
record_id = '1'
Output:
{
    'customers': {}
}
Explanation: Customer '1' (Alice) is deleted, but no orders are associated with them, so no orders are deleted.

Example 3: (Edge Case)

Input:
data = {
    'customers': {
        '1': {'name': 'Alice', 'city': 'New York'}
    },
    'orders': {
        '101': {'customer_id': '1', 'item': 'Book'},
        '102': {'customer_id': '1', 'item': 'Laptop'}
    }
}
record_id = '3'
Output:
{
    'customers': {
        '1': {'name': 'Alice', 'city': 'New York'}
    },
    'orders': {
        '101': {'customer_id': '1', 'item': 'Book'},
        '102': {'customer_id': '1', 'item': 'Laptop'}
    }
}
Explanation: Customer '3' does not exist, so the data remains unchanged.

Constraints

  • data will always be a dictionary with 'customers' and 'orders' keys.
  • record_id will be a string.
  • The customer_id in the orders table will be a string.
  • The number of customers and orders will be less than 1000.
  • The function must have a time complexity of O(n), where n is the total number of records across both tables.

Notes

  • Consider iterating through the orders table to find related records.
  • Remember to modify the data dictionary in place.
  • Handle the case where the record_id is not found in the customers table.
  • Think about how to efficiently remove records from the dictionaries. del is a good option.
Loading editor...
python