"ContentType matching Query does not exist" - only on SQLite, not MySQL "ContentType matching Query does not exist" - only on SQLite, not MySQL sqlite sqlite

"ContentType matching Query does not exist" - only on SQLite, not MySQL


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.