Bi-directional ManyToMany with through gets broken when creating test database

66 views
Skip to first unread message

filias

unread,
Dec 4, 2015, 3:54:35 AM12/4/15
to Django users
Hi,

I have recently upgraded to sjango 1.8 and I have 2 models with a bi-directional ManyToMany field. It looks like this

class Pizza(Model):
    available_toppings = ManyToManyField('Topping')

class Topping(Model):
   all_pizzas = ManyToManyField(Pizza, through=Pizza.available_stoppings.through, verbose_name='available pizzas')

Everything was fine in django 1.7 but right now when running tests, specifically when creating the test database, I get this exception:

self = <django.db.backends.sqlite3.base.SQLiteCursorWrapper object at 0x7fd24fca4b00>

    def execute(self, query, params=None):
        if params is None:

>           return Database.Cursor.execute(self, query)
E           OperationalError: table "products_pizza_available_toppings" already exists

So it looks like the creation of the test database is trying to re-create the intermediate table for the ManyToMany.

I do not get any exception when running my site.

Does anyone know how can I fix this?

Thank you in advance

Remco Gerlich

unread,
Dec 4, 2015, 5:31:19 AM12/4/15
to django...@googlegroups.com
One, from that error message alone I can't figure out what is happening and why when you run tests, so I can't help there.

But two: you don't have a ManyToManyField, you have TWO ManyToManyFields. If you define a many to many field on one model, than it's automatically also defined on the other (if you only have the one on Pizza, then you can do topping.pizza_set.all() from the other side.

So your current code is almost certainly wrong, but if these are your exact models than I don't think they are related to your error.

Remco

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4b8bb3c4-c0bd-4315-9cb6-004db2fa2355%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

filias

unread,
Dec 4, 2015, 7:08:51 AM12/4/15
to Django users
Indeed something in the project's code must be wrong but it is not the bi-directional ManyToMany as this has been working in django 1.4, 1.5, 1.6 and 1.7. One might want to had the ManyToMany in both models so to be able to add more information, as it is stated in the django docs https://docs.djangoproject.com/en/1.9/topics/db/models/#extra-fields-on-many-to-many-relationships in my case I just want the field to have a different name. I am thinking of using a property but I got into some troubles with my code when I did that, specifically the migration to remove the all_pizzas field crashes.

I have created a new project with only those 2 models and it seems to work correctly, so the problem is somehow related to our test runner (py.test) and something else which I still don't know.

My models.py file looks like this:
from django.db import models


class Pizza(models.Model):
    available_toppings = models.ManyToManyField('Topping')


class Topping(models.Model):
   all_pizzas = models.ManyToManyField(Pizza, through=Pizza.available_toppings.through, verbose_name='available pizzas')

I will continue investigating.

Simon Charette

unread,
Dec 4, 2015, 10:38:41 AM12/4/15
to Django users
Hi filias,

I personally didn't know one could define a reverse foreign key this way and
I doubt this use case is covered by the test suite. I'm afraid it worked for
Django < 1.8 by chance since it's not documented you can re-use the
implicitly created intermediary model as a `through` argument. I suspect this
might be due to the `_meta`[1] refactoring that happened in 1.8.

May I ask you why you don't simply define a `related_name`[2] if you simply
want to have a different reverse relationship name?

class Pizza(models.Model):
    pass

class Topping(models.Model):
    all_pizzas = models.ManyToManyField(
        Pizza, related_name='available_toppings',
        verbose_name='available pizzas'
    )

Cheers,
Simon

[1] https://docs.djangoproject.com/en/1.8/releases/1.8/#model-meta-api
[2] https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.ManyToManyField.related_name

filias

unread,
Dec 4, 2015, 1:28:44 PM12/4/15
to Django users
Hi Simon,

thanks for your reply, I suspected exactly what you said.
I am not sure, this is not my code so I am not aware of the reason for this but I will try to refactor it the way you suggest.

Cheers,
filipa
Reply all
Reply to author
Forward
0 new messages