Home > database >  Does this many-to-many relationship make sense?
Does this many-to-many relationship make sense?

Time:04-17

I'm writing a django app and I have the following models below, however I am unsure about the relatinonships of order_id and product_id in DetailedOrder and customer_id in Demand model.

In the DetailedOrder model, there can be multiple order_id (O0001) if the customer orders multiple product_id. Then the order_id can be used in the Order model to find who the customer was.

Or should the product_id in the DetailedOrder be a many to many relationship because there can be multiple products for 1 order_id - i think this makes more sense.

Also by this logic, does this mean customer_id in the Ordermodel should be a many-to-many relationship because there can be multiple customer_ids to multiple order_ids?

Any advice is appreciated!

class Customer(models.Model):
    customer_id = models.CharField(primary_key=True, max_length=150)
    customer_name = models.CharField(max_length=150, null=True)

class Product(models.Model):
    product_id = models.CharField(primary_key=True, max_length=100)
    product_name = models.CharField(max_length=150)

class Order(models.Model):
    order_id = models.CharField(primary_key=True, max_length=100)
    customer_id = models.ForeignKey(Customer, null=True, on_delete= models.SET_NULL)


class DetailedOrder(models.Model):
    order_id = models.ForeignKey(Demand, null=True, on_delete= models.SET_NULL)
    product_id = models.ForeignKey(Product, null=True, on_delete= models.SET_NULL)
    quantity = models.IntegerField()

Updated for Willem comment:

   order_id = models.ManytoManyField(Demand, null=True, on_delete= models.SET_NULL)
   product_id = models.ForeignKey(Product, null=True, on_delete= models.SET_NULL)
   quantity = models.IntegerField()```

CodePudding user response:

Your DetailOrder acts as the through model of a ManyToManyField. You can for the same Order have multiple DetailedOrders, and thus also refer to multiple Products.

You can also span a ManyToManyField [Django-doc] over this model to effectively find out the Products, with DetailedOrder as the through=… model [Django-doc]:

class Product(models.Model):
    product_id = models.CharField(primary_key=True, max_length=100)
    product_name = models.CharField(max_length=150)

class Order(models.Model):
    order_id = models.CharField(primary_key=True, max_length=100)
    customer_id = models.ForeignKey(Customer, null=True, on_delete= models.SET_NULL)
    products = models.ManyToManyField(
        Product,
        through='DetailedOrder',
        related_name='orders'
    )

class DetailedOrder(models.Model):
    order = models.ForeignKey(Order, null=True, on_delete= models.SET_NULL)
    product = models.ForeignKey(Product, null=True, on_delete= models.SET_NULL)
    quantity = models.IntegerField(default=1)

Note: Normally one does not add a suffix …_id to a ForeignKey field, since Django will automatically add a "twin" field with an …_id suffix. Therefore it should be product, instead of product_id.

  • Related