gpt4 book ai didi

python - 数据库设计正确

转载 作者:行者123 更新时间:2023-11-29 04:20:05 24 4
gpt4 key购买 nike

我正在创建一个与牙医有关的 Django 项目。每个客户都有自己的一套 dentry ,所以我有一个模型 Customer 和一个带有客户外键的模型 Tooth

有一个称为牙周图的检查,它存储了每颗 dentry 的一些信息,例如牙龈边缘探测深度(如果它有植入物( dentry )等)。我想在我的项目中实现它。考虑到客户可能有不止一张牙周图,我决定将其作为一个模型来实现。每个牙周图模型都有许多牙周图操作,其中每个操作都包含有关每颗 dentry 的信息。所以它看起来像这样

class Customer(models.Model):
.....
Class Tooth(models.Model):
customer = models.ForeignKey(Customer)


class Periodontogram(models.Model):
customer = models.ForeignKey(Customer)
date = models.DateField()

class PeriOperation(models.Model):
tooth = models.ForeignKey(Tooth)
peri = models.ForeignKey(Periodontogram)
#rest of work done per tooth
implant = models.BooleanField()
ginginal_margin1 = models.IntegerField()
#...more

前端发送json数据到后端保存。所以为了正确保存它,我必须

  1. 创建新的牙周图并将其保存到数据库
  2. 为每颗 dentry 创建手术(从前端获取)
  3. 将 periOperation 的周边区域分配给在步骤 1 中创建的牙周图

但是,如果在为每个 dentry 创建手术时某些手术失败了,会发生什么情况。周边将被保存,但不会进行任何操作。处理它的最佳方法是什么?

最佳答案

您应该使用数据库事务,以便所有数据都保存到数据库中,否则什么都不保存。您可以在此处阅读有关交易的更多信息

https://docs.djangoproject.com/en/dev/topics/db/transactions/

您可能还需要检查 MySql 是如何处理事务的:

http://dev.mysql.com/doc/refman/5.0/en/commit.html

关于python - 数据库设计正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26800958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com