I am developing online shopping cart system, I have items and item_options tables.
Each Item can have 1 or more options, see below.
items table
- item_id (PK)
- item_name
- item_description
Note: item prices are in the item_options tables
item_options table
- option_id (PK)
- item_id (FK)
- option_name
- option_price
When the customer placed an order, the data will be added into the order and order_items table.
orders table
- orders_id (PK)
- customer_id (FK)
- address_address_id (FK)
- date_purchased
- orders_status
- orders_date_finished
order_items table
- orders_items_id (PK)
- orders_id (FK)
- item_id (FK)
- item_option_id (FK)
There is a problem, if the staffs change the price, item name or delete a item.. the customer invoices will be affected because the FK from the order_items will no longer be existed in the items table. What is the solution to this?
How about this solution: add active field in the items and item_options tables. When the staffs want to change the price or name of a item – just turn the current active record to 0 (items.active) and then insert a new record with the new information and active become 1. The old orders still point the correct id of the entry that is not active. Is this good way doing it?
When the new information has been inserted in the items table, does that mean I have to update/change the new PK ID in the item_id.item_options table?
In something like this, I am definitely against the use of hard deletes. Every update or delete should be adding a new row into the item_options table and then marking the previous one as inactive (that way there is no chance that a user can select one of the previous values).
Each new item inserted should consist of two steps: