Take the 2-minute tour ×

Django (version 1.1.4 for what it's worth) is arbitrarily not finding items that are in one of my database tables. The objects manager in question is the default one. Two examples follow, the first showing the scope of the problem, the second providing a particular instance of it.


In [5]: LocalStore.objects.all().count()
Out[5]: 1466


mysql> select count(*) from stores_localstore;
| count(*) |
|     1553 | 
1 row in set (0.00 sec)

Strange enough by itself. Now here's an example of a single object that's in the DB but isn't showing up:


In [37]: LocalStore.objects.filter(id=474361)
Out[37]: []
In [39]: print LocalStore.objects.filter(id=474361).query
SELECT `stores_localstore`.`id`, `stores_localstore`.`cust_id`,
`stores_localstore`.`name`, `stores_localstore`.`lat`, `stores_localstore`.`lng`,
`stores_localstore`.`addr1`, `stores_localstore`.`addr2`, `stores_localstore`.`city`, 
`stores_localstore`.`email`, `stores_localstore`.`fax`, `stores_localstore`.`phone`, 
`stores_localstore`.`state`, `stores_localstore`.`url`, `stores_localstore`.`zip` FROM
`stores_localstore` WHERE `stores_localstore`.`id` = 474361 

For privacy reasons, I don't want to show you the exact result of the MySQL query, but it should suffice to say that it does return a database row.

This strikes me as very strange behavior and potentially a bug deep in the bowels of the Django ORM. But it's also possible that I'm overlooking something obvious. What could that be?

share|improve this question
Single MySQL server or pooled/replicated/multi-db setup? Have you tried it with just get()? Is there some eager queryset cacheing going on (eg via Cache-Machine or Johnny Cache or some other drop-in lib)? –  stevejalim May 17 '12 at 12:18
What is the MySQL version and db engine you are using? –  Burhan Khalid May 17 '12 at 12:20
Did any of those rows go in manually/not via Django? If so, is it possible somehow that the index for that table is out of date? –  stevejalim May 17 '12 at 12:21

2 Answers 2

up vote 1 down vote accepted

Django is either looking at a different database than you're connected to, or you have told django to cache somehow. Django does not 'lose' data.

Validate that django is connected to the right database:

from django import db
share|improve this answer
The simple answer is indeed the correct one. As I suspected, I was missing the obvious. Somehow "dbshell" and "shell" were taking me to different MySQL databases. What a relief. Thank you, sir. –  Matthew Leon May 17 '12 at 20:09
@MatthewLeon shell and dbshell really should take you to the same database - have you worked out how this happened? Differently loaded settings file, or perhaps a database router interfering? –  Josh Smeaton May 17 '12 at 22:43
Yeah, settings file that was loaded through having the wrong PYTHONPATH, I believe. –  Matthew Leon May 17 '12 at 23:57

Possibly the isolation level of your MySQL is REPEATEABLE READ(which is default for innodb), and the DB connection in Django shell is in a transaction earlier than the new inserting of rows(caused by manually transaction management or using of some connection-pool, etc) and still holds an expired snapshot of old rows.
Then you experienced phantom reading.

share|improve this answer

Your Answer


