Tom Evans wrote:

> Phlip, I'm going to try and make a non-stupid comment now :)

http://xkcd.com/386/

> If you already know precisely the query you want to use, and you can't
> coerce django's ORM to produce it, can you simply use Manager.raw()[1]
> to generate the result set you are after?

Because the point of an ORM is to distribute declarations of relations
among objects, so each object adds details to a query set, and the ORM
can build the final SELECT statements for each context.

So anyway, here's a table (hand-censored - it's a blue-sky project in
a hyper-competitive space):

> select * from things;
+-----+-----------------+
| pid | name            |
+-----+-----------------+
|   6 | soca_2k7_user   |
|   7 | isabelle_item   |
|   8 | max_item        |
|   9 | isabelle_item   |
+-----+-----------------+

Note that "isabelle_item" appears twice. We are following the auditing
rule "always write new records to change data - never edit previous
records". Someone edited isabelle_item's payload data (not shown), so
we add a new record without touching the existing record.

This implies that all normal database queries should only look at the
"top level horizon" of the database. (And this implies we must mix-and-
match such queries, and they can't all rely on the order_by('-pk')[0]
trick.)

So this statement correctly fetches only the latest items:

SELECT a.* FROM things a WHERE a.pid in (select max(b.pid) from
content_entity b group by b.name)

Now I thought (from my allegedly copious experience with SQL) that I
could do it with a join-on-self, but I can't seem to get the SQL
syntax right. And if I did, I would then not know how to ORM-ize that
syntax (and yes it must be ORM-ized, because this is indeed the core
of the project, and everything has to see top-level horizons. Except
auditors).

--
  Phlip
  http://zeekland.zeroplayer.com/

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to