1

How to make a one to many relationship in Django/Mysql?

I have an identical situation to this post, yet, my django returns errors on the admin page:

get() returned more than one order2pizza-- it returned 5!

order2pizza with that pizza already exists.

My mysql database have composite keys on a tertiary table to order and pizza to link multiple pizzas to an order.

models.py:

class Orders(models.Model):
    order_id = models.AutoField(primary_key=True)
    order_name = models.CharField(max_length=100, blank=True, null=True)


class Pizza(models.Model):
    Pizza= models.AutoField(primary_key=True)
    Pizza_name= models.CharField(max_length=50, blank=True, null=True)

class order2pizza(models.Model):
    order = models.ManyToManyField(Orders, models.DO_NOTHING, )
    pizza_id= models.IntegerField()
    class Meta:
        unique_together = (('order ', 'pizza_id'),)

Shadow
  • 33,525
  • 10
  • 51
  • 64
Kari dime
  • 78
  • 11
  • 2
    Why do you want to create another model for relating pizza and order? You can directly use the `ManyToManyField` in your `Pizza` model. like this : `orders = models.ManyToManyField(Orders, blank=True, null=True)`. – Ajay Lingayat Jan 26 '21 at 21:55
  • 1
    @Ajay Interesting!, but i need to use the actual relations already estabilished by the db, how can i migrate this on mysql, is there a way that you can guide me to? – Kari dime Jan 26 '21 at 22:38
  • 1
    Where did you see the `get() returned more than one order2pizza` error? – Jaap Joris Vens Jan 26 '21 at 23:17
  • 1
    @Jaap I got it after clicking on an item inside order2pizza admin – Kari dime Jan 27 '21 at 00:21

1 Answers1

2

A many-to-many relation can be expressed in two ways. First, you can manually specify a "join" model, like this:

class Orders(models.Model):
    order_name = models.CharField(max_length=255, blank=True)

class Pizza(models.Model):
    Pizza_name= models.CharField(max_length=255, blank=True)

class Order2Pizza(models.Model):
    order = models.ForeignKey(Order, models.CASCADE)
    pizza = models.ForeignKey(Pizza, models.CASCADE)
    class Meta:
        unique_together = ['order ', 'pizza']

This is useful if you want to put extra fields on the Order2Pizza model. A field named quantity would be very useful in your example.

The second option is to use a ManyToManyField. This will automatically create the join model for you:

class Orders(models.Model):
    order_name = models.CharField(max_length=255, blank=True)
    pizzas = models.ManyToManyField('Pizza', related_name='orders')

class Pizza(models.Model):
    Pizza_name= models.CharField(max_length=255, blank=True)

In your original question you put the ManyToManyField on the Order2Pizza model, which is nonsensical.

However, the source of your bug is probably your manual inclusion of several *_id fields. Don't do that. They will always be created automatically by Django and you should never have to specify them manually. Instead, try the two options above and see how they work.

Jaap Joris Vens
  • 3,382
  • 2
  • 26
  • 42