Table Aliases?
Gustavo Niemeyer
gustavo at niemeyer.net
Tue Jul 17 14:47:59 BST 2007
Hello Akira,
> How does one declare and use table aliases?
Excellent question.. I've added another topic to the tutorial.
Copied below. Please let me know if that answers the question
properly.
==== Aliases ====
So now let's say that we want to find every pair of people that work
for the same company. I have no idea about why one would ''want'' to
do that, but that's a good case for us to exercise aliases.
First, we import `ClassAlias` into the local namespace (''mental note:
this should be in storm.locals as well''), and create a reference to it.
>>> from storm.info import ClassAlias
>>> AnotherEmployee = ClassAlias(Employee)
Nice, isn't it?
Now we can easily make the query we want, in a straightforward way:
>>> result = store.find((Employee, AnotherEmployee),
... Employee.company_id == AnotherEmployee.company_id,
... Employee.id > AnotherEmployee.id)
>>> for employee1, employee2 in result:
... print (employee1.name, employee2.name)
(u'Mike Mayer', u'Ben Bill')
Woah! Mike and Ben work for the same company!
(Quiz for the attent reader: why is ''greater than'' being used in
the query above?)
--
Gustavo Niemeyer
http://niemeyer.net
More information about the storm
mailing list