Reputation: 27186
In Django, I have a model, let's call it "MyTable" which uses a content_type Foreign Key to refer to, amongst other things, Profile.
In most cases (as in my unit-tests) I have no trouble with it, but in a certain circumstance in the view I try to save a Profile object (with Profile's .save() ) and the database throws this exception :
relation "_mytable" does not exist
I presume that this is because of some reverse-lookup that the ORM is making between the Profile and MyTable due to the ForeignKey from MyTable to Profile.
Now there definitely is a relation in the database called myapp_mytable. But in this case, the ORM seems to have lost the app-name. Checking the SQL confirms this, it really is trying to select from _mytable instead of myapp_mytable.
Anyone seen anything like this or have suggestions?
Upvotes: 2
Views: 2188
Reputation: 43840
I imagine you resolved or worked around this by now, but I ran across the same.
My case sounds a little different than yours, but it took me several hours to figure out so I'll post the issue here for reference.
In my case, I had code in my model that was expecting data to be pre-loaded in the database and it wasn't.
The query appeared to work fine when run from shell, however, when I ran the same code in the unittest it would fail with the "relation not found" error.
The reason was that, when run from shell the data I was expecting was already loaded in my DB. However, in the testcase situation (the "manage.py test" behavior is to create an empty db before test) , while I was defining the testcase's fixture, the fixture is not loaded prior to model validation/evaluation. So the expected fixture data didn't exist when I was expecting it.
Upvotes: 3