订单、订单项目、产品。将产品详细信息写入 OrderItem?

Order, OrderItem, Product. Write Product details to OrderItem?

这不是关于特定技术问题的问题,而是关于数据库设计的一般问题。尽管如此,技术堆栈是:ASP.NET MVC,SQL DB。

我最近继承了一个系统,它有一个订单-->订单项目-->产品概念,即一个订单有很多订单项目,每个订单项目都与一个产品相关联。

订单项在保存到数据库时存储以下内容:

  • OrderID
  • ProductID
  • Qty
  • Unit Cost (written from Product)
  • Total NET (calculated based on Qty * Unit Cost)
  • VAT
  • Total GROSS

通过 UI 查看的订单商品如下所示:

Qty | ProdCode | ProdDescription | Unit Cost | Total NET | VAT | Total Gross

数量、单位成本和总计都从订单项目中提取到 UI 中,产品代码和产品描述从关联的产品记录中提取。

一切似乎都足够明智。

所以我的问题围绕着哪些数据应该从产品中写入,哪些数据应该从产品中引用。具体来说,单位成本是在创建订单项目时从产品中写入的。我认为这是因为产品价格发生变化,而您不希望将此更改应用于旧订单。很好,有道理。

My question is: should the same logic should also apply to Product Code and Product Description? And if not, why not?

To my mind, it seems like the Product Code and Description should also be getting written to the Order Item i.e. the Product Code and Description are as liable to change as the Product Unit Cost. In which case if you were to go back and look at an old order, the Prod Code and Description on the order would appear to be different to what was originally ordered, which seems wrong to me.

构建系统的开发者已经无法讨论他设计时的想法。

系统运行良好,没有任何投诉。然而,这主要是因为 Prod Codes/Descriptions 从未有过任何更新,尽管它们可供各种用户编辑。

在我进行大规模更改之前,我很想听听人们对此的想法,这是一种常见的情况吗?我什么都不担心吗?

在这种情况下,需要考虑几个方面。 让我们从订单必须是 immutable 的事实开始。

由于产品代码和描述不是immutable,乍一看,将它们保留在订单table中似乎是有意义的。
但是,对于每个订单中的每个产品,这可能会导致大量重复数据。

另一种方法是永远不要保留产品代码和描述 immutable。

另一种方法是允许管理员编辑产品代码和描述,但不是更新产品中的行 table 您只需将其标记为历史记录(您需要为那,当然)并为该产品添加一个新行,其中包含新代码和描述。
此解决方案将允许您保持订单的完整性,同时允许您的管理员用户编辑他们想要的任何内容,并保持最少的数据,特别是如果对产品代码或描述的更改像您写的那样稀疏。