2

I'm experiencing a strange bug with the way Django Test framework operates.

When using SQLite Database Backend, all of the tests crash with the following error:

  File "[]/core/tests/test_admin.py", line 91, in setUpSomething
    content_type = ContentType.objects.get(app_label='core', model='SomeModel')
  File "[]/lib/python2.7/site-packages/django/db/models/manager.py", line 151, in get
    return self.get_queryset().get(*args, **kwargs)
  File "[]/lib/python2.7/site-packages/django/db/models/query.py", line 310, in get
    self.model._meta.object_name)
DoesNotExist: ContentType matching query does not exist.

However, the same code executes well under MySQL backend.

Clearly Django should make these functions agnostic of the backend used?

qdot
  • 6,195
  • 5
  • 44
  • 95
  • This answer may help: http://stackoverflow.com/a/17614700/2011147 – Selcuk Mar 22 '15 at 12:27
  • I've tested this approach, and it doesn't work.. might be some transaction peculiarities of Django TestCase? – qdot Mar 23 '15 at 14:56
  • Same issue here: code works on MySql, but doesn't work with SQLite test database. I assumed that some version of `syncdb` was being used to set-up the db and that should update all ContentTypes model instances, but apparently not. Will investigate further. – erewok Aug 17 '15 at 18:48

1 Answers1

2

I had the same problem and I have no idea if my solution will be helpful, but it solved my issue, so here goes.

In my application code, I was attempting to query ContentType instances in the following way:

email = ContentType.objects.get(app_label="users", model="EmailAddress")

This worked fine with our actual MySQL database, but failed in test under a SQLite test database. However, if I switched the model definition to lowercase, it worked in both places:

email = ContentType.objects.get(app_label="users", model="emailaddress")

My guess was that this may have to do with the default collation in MySQL of case-insensitivity, so the first query should not have worked if I were comparing case-sensitively.

Indeed, when I looked at my database, all the model labels in the django_content_type table were lowercase and SQLite cares (by default) about case, so my queries in my tests were legitimately failing.

Community
  • 1
  • 1
erewok
  • 7,555
  • 3
  • 33
  • 45