Search code examples
zodb

Is there a primary key concept in ZODB for objects


I wanted to know if we can have a unique identifier for all objects that are stored in ZODB


Solution

  • Each object stored in the ZODB does have a unique ID, accessible as its _p_oid attribute after the object has been registered:

    >>> obj._p_oid
    

    The usual caveats about primary keys apply -- if you need a unique identifier that you can continue to use even if you need to export your objects to a different database, you're better off using a uuid that you manage yourself. Python has a uuid module (in the stdlib as of Python 2.6) which can generate uuids.