[Zope] Trying ZODB with a background in RDBMS

Chris Withers chris at simplistix.co.uk
Sat Aug 16 18:09:54 EDT 2008


Phillip B Oldham wrote:
> Hi all. I'm playing with standalone ZODB at the moment trying to get a
> better understanding of its use in applications. I come from a
> PHP/MySQL background, and I'm taking my first steps with Python at the
> same time.

Heh, and just when people are moving more and more to ORMs like 
SQLAlchemy ;-)

> One of the things I'm not understanding about ZODB is assigning
> incremental IDs to objects.

How important is it that they're strictly incremental?
If it is, you may want to look at the BTrees.Length module.
You can treat a Length a bit like a relational database sequence, 
provided you keep incrementing each time you use the Length's value as 
an id.

> For instance, if I were to be writing a
> support-ticket system I'd want to give each ticket a unique number,
> but one that's "human-useable" (otherwise you could just use a UUID -
> try giving one of those over the phone!).

Well, the UUID you'd be thinking of would best be just the ZODB OID ;-)

> class Ticket(Persistence):
>   def __init__(self):
>     self.id = '' # How do I add a new incremental ID here?
> 
> # and later on in the app
> 
> tkt = Ticket()
> dbroot[?????] = tkt

Well, if it was just plain ZODB, I'd do it as follows:

from persistent import Persistent
from IOBTrees import IOTree
from BTrees.Length import Length

class Ticket(Persistent):
   pass

class TicketContainer(IOBTree):

   def __init__(self,id):
     IOBTree.__init__(self)
     self.next_id = Length()

   def addIssue(self):
     id = self.next_id()
     self.next_id.change(1)
     t = self[id] = Ticket()
     return t

dbroot['tickets']=TicketContainer()

It's very rare that a ticket needs to know its own id. If it does, it 
should ask its container.

cheers,

Chris

-- 
Simplistix - Content Management, Zope & Python Consulting
            - http://www.simplistix.co.uk


More information about the Zope mailing list