summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tom Willemsen2012-11-04 12:23:24 +0100
committerGravatar Tom Willemsen2012-11-04 12:23:24 +0100
commit0bdbf1641989b2c1417ac04a8b1c80232598b935 (patch)
treeb9093305bfdaf0b47ed62b852985da014df6953a
parentea98325f1e27cd4a01474ab01df2b43f6a1251dd (diff)
downloadsite-0bdbf1641989b2c1417ac04a8b1c80232598b935.tar.gz
site-0bdbf1641989b2c1417ac04a8b1c80232598b935.zip
Add "Silly django confusion" archive post
-rw-r--r--posts/Silly_django_confusion.mdwn38
1 files changed, 38 insertions, 0 deletions
diff --git a/posts/Silly_django_confusion.mdwn b/posts/Silly_django_confusion.mdwn
new file mode 100644
index 0000000..3939fdb
--- /dev/null
+++ b/posts/Silly_django_confusion.mdwn
@@ -0,0 +1,38 @@
+I'm setting up a testing environment for work, using fixtures to save
+and load test data and I got a little stumped by something I ran into.
+
+I had exported one of the database tables we use to a json file that I
+was going to import into a fresh new database to test with. When I
+imported it everything seemed fine, except when looking at the actual
+page. So this is what I found:
+
+<pre class="src src-sql">SELECT * FROM app_table;
+ =&gt; 3 rows of data</pre>
+
+<pre class="src src-python"><span class="org-keyword">from</span> app.models <span class="org-keyword">import</span> Table
+
+Table.objects.count()
+ =&gt; 3
+
+Table.objects.<span class="org-builtin">all</span>()
+ =&gt; []</pre>
+
+This is weird. So I looked at the `django.db.connection.queries`
+property and I realized that it was doing a join since the model
+subclasses another:
+
+<pre class="src src-python"><span class="org-keyword">from</span> django.db <span class="org-keyword">import</span> models
+
+<span class="org-keyword">from</span> app.models <span class="org-keyword">import</span> SuperTable
+
+<span class="org-keyword">class</span> <span class="org-type">Table</span>(SuperTable):...</pre>
+
+
+Which, of course, means that in order to get the complete `Table`
+instance, the related `SuperTable` instance is also required, but in
+order to do a `COUNT` of `app_table` it isn't necessary. And that's
+where the inconsistency came from, now that I've also copied the
+contents of `SuperTable` everything is fine again.
+
+[[!meta date="2012-04-24 15:51:00"]]
+[[!tag python django coding]]