外键约束是强制性的吗?为什么不只保留密钥而不保留约束?

时间:2021-07-17 04:01:12

标签: django django-models django-rest-framework

外键约束是强制性的吗?为什么不只保留密钥而不保留约束?

例如:

class SalesOrderSummary(models.Model):

    id = models.BigAutoField(primary_key=True)
    uuid = models.UUIDField(default=uuid.uuid4, editable=False)

    # Order Status values could : CONFIRMED, DISPATCHED, DELIVERED
    order_status = models.CharField(max_length=255, blank=False, default='')


class SalesOrderItem(models.Model):
    """
     Line item details of the particular sales order, mostly they are grouped by seller, for easy processing
    """

    id = models.BigAutoField(primary_key=True)
    uuid = models.UUIDField(default=uuid.uuid4, editable=False)

    # Here I don't keep the ForeignKey Constraint, but I understand the constraint
    # and manage it myself, like whenever Order is deleted, these items are also 
    # getting deleted
    sales_order_id = models.BigIntegerField()

    seller_sku = models.CharField(max_length=255, blank=False, default='')
    product_title = models.CharField(
        max_length=255, blank=False, default='')
    quantity = models.BigIntegerField()
    unit_price = models.DecimalField(
        max_digits=10, decimal_places=2, default='0.00')

我完全同意这些 FK 约束是为了让我们的生活更轻松,让我们完成我们的工作。比如当关联的主实体被删除时自动删除。如果我自己能做到,那么问题出在哪里。因此,如果我会遇到任何死胡同或某些根本不可能的功能,请告诉我。

0 个答案:

没有答案
相关问题