django - 数据建模 - 发票和行项目

标签 django database-design data-modeling

我正在创建一个基于 Web 的销售点(例如收银机)解决方案,并以 Django 作为后端。我一直采用“经典”方法对发票及其行项目进行建模。

InvoiceTable
  id
  date
  customer
  salesperson
  discount
  shipping
  subtotal
  tax
  grand_total
  [...]

InvoiceLineItems
  invoice_id // foreign key
  product_id
  unit_price
  qty
  item_discount
  extended_price
  [...]

在尝试研究最佳实践后,我发现没有很多 - 至少没有广泛使用的明确来源。

Kimball Group 建议:“我们建议您将标题的所有维度都归结为行项目,而不是坚持交易标题“对象”的操作概念。”

参见http://www.kimballgroup.com/2007/10/02/design-tip-95-patterns-to-avoid-when-modeling-headerline-item-transactions/http://www.kimballgroup.com/2001/07/01/design-tip-25-designing-dimensional-models-for-parent-child-applications/ .

我是开发新手(之前只使用过桌面数据库软件) - 但根据我的理解,这是有道理的,因为我们可以以任何我们想要的方式钻取数据以用于报告目的(尽管我想我们可以做同样的事情)第一种方法是连接表)。

我的问题

  • 每行都需要重复发票 ID(以便我们可以生成发票总计等数据)。这是这种数据建模方式的故意特征吗?

  • 我们经常有发票级别的数据,例如备注、折扣、运费等。 - 我们如何使用此方法表示这些数据?有些折扣是特定于产品的 - 因此它们无论如何都属于订单项,其他折扣则属于发票范围(想想您购买两种单独的产品并获得这两种产品的折扣的交易) - 我们可以以某种方式将其分配给订单项吗?与运费相同,通过将其划分到各行项目中来分配吗?

  • 我们如何处理发票“注释” - 我们已经打印和/或内部注释,我们是否会将数据放入行项目中,然后对每个行项目重复它?这似乎违背了数据标准化。将其放入相关表中吗?

    有任何使用这种方法的开源项目我可以看一下吗?不知道如何搜索它们。

最佳答案

听起来您混淆了关系设计和维度设计。

关系设计是为了促进事务处理,并最大限度地减少数据异常和重复。这是您的操作数据库。尺寸设计是为了便于分析。

关系设计将具有发票表和 line_items 表,维度设计将具有带有发票行项目粒度的 company_invoices_customer 事实表。

由于这是针对 POS 的,我假设您首先需要一个关系设计。

至于你的问题:

首先,针对此场景有大量良好的数据建模模式。请参阅https://dba.stackexchange.com/questions/12991/ready-to-use-database-models-example/23831#23831

The invoice ID will need to be repeated for each row (so we can generate data like totals for the invoice). Is this an intentional feature of this way of modeling the data?

是的

We often have invoice level data like notes, discounts, shipping charges, etc. - How do we represent these using this method?

在发票表上有一个“注释”字段可能是最简单/最简单的。

对于费用和折扣,您应该使用抽象(请参阅表继承),并将它们添加为订单调整。请参阅上面链接中西尔弗斯顿所著的书。

Some discounts are product specific - so they belong on the line item anyway, others are invoice wide (think of a deal where you buy two separate products and receive a discount on the two) - we could we somehow allocate it across the line items?

商品的价格应在运行时根据其默认价格以及当前“场景”中适用的任何折扣或费用进行计算,例如政府折扣、附近的促销日折扣。您可以使用相互引用的分层行项目,以保持秩序。再次参见西尔弗斯顿的书。

What do we do with invoice 'notes' - we have printed and/or internal notes, would we put the data in the line items and just repeat it for each line item?

如果您需要订单项注释,请在订单项表格上添加注释列。

That seems to go against data normalization. Put it in a related table?

如果注释可以为空,并且您希望严格规范化,那么可以添加一个invoice_notes 表。

关于django - 数据建模 - 发票和行项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23881172/

相关文章:

json - 如何在 Airtable 中存储一团 JSON?

django - 基于 cookie 在两个服务器之间路由流量

python - 使用 Django 在 View 中对数据库的查询量

database - 什么时候适合将 UUID 用于 Web 项目?

c# - 当读取远高于写入时将计算值存储在数据库中

cassandra - 在 cassandra 中存储 bool 值的更好方法是文本列还是 bool 列?

应用程序日志的 Cassandra 数据模型(数十亿次操作!)

python - Django 项目 : django-admin. py:找不到命令

mysql - 我想制作json数据

c# - 存储此类信息的适当方式