seymore_strongboy
seymore_strongboy

Reputation: 964

Schema Best Practice: Is it ever OK to store duplicates of a record?

This is a general question about good schema design. In my case I'm using Rails 4 and PostgreSQL.

Let's say I have a storefront, with many products. The store also has many orders, each with many products.

On Monday, a customer pays for an order. So now, we have a product that belongs to both a storefront and an order.

On Tuesday, the store decides to change the price of one of the products.

Clearly, I don't want to change the record of what the customer purchased on Monday, so this leads me to believe I should store two copies of the product - one copy of what the store is selling now (ie Tuesday), another copy for what the customer bought on Monday.

However, I know that duplicating data like this something generally to be avoided.

Is there a best-practice for handling a situation like this?

Upvotes: 1

Views: 42

Answers (1)

Mori
Mori

Reputation: 27779

That only sounds like duplication because you're using the same name for them. One price is the price the customer paid. The other is the price a product is currently sold at. They're separate pieces of data, even though in a lot of the cases they have the same value. I'd just give them separate names (e.g. Order#paid v. Product#price), and store the Product#price in Order#paid at the time of the sale. That way the product price can be change however needed without affecting the order.

Upvotes: 2

Related Questions