Re: Trying ZODB, background in Relational: mimic auto_increment?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jean-Paul Calderone

    Re: Trying ZODB, background in Relational: mimic auto_increment?

    On Thu, 14 Aug 2008 05:22:35 -0700 (PDT), Phillip B Oldham <phillip.oldham @gmail.comwrote :
    >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.
    >
    >One of the things I'm not understanding about ZODB is assigning
    >incremental IDs to objects. 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!).
    >
    >Also, how would one add a new item to the db in this way?
    >
    >For instance:
    >
    >class Ticket(Persiste nce):
    def __init__(self):
    self.id = '' # How do I add a new incremental ID here?
    >
    ># and later on in the app
    >
    >tkt = Ticket()
    >dbroot[?????] = tkt
    >
    >How would one assign a unique ID to the root at that point?
    Here's one way

    class Sequence(Persis tence):
    def __init__(self):
    self.current = 0

    def next(self):
    self.current += 1
    return self.current

    ticketSequence = Sequence()

    class Ticket(Persiste nce):
    def __init__(self):
    self.id = ticketSequence. next()

    Jean-Paul
  • Diez B. Roggisch

    #2
    Re: Trying ZODB, background in Relational: mimic auto_increment?

    Jean-Paul Calderone wrote:
    On Thu, 14 Aug 2008 05:22:35 -0700 (PDT), Phillip B Oldham
    <phillip.oldham @gmail.comwrote :
    >>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.
    >>
    >>One of the things I'm not understanding about ZODB is assigning
    >>incremental IDs to objects. 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!).
    >>
    >>Also, how would one add a new item to the db in this way?
    >>
    >>For instance:
    >>
    >>class Ticket(Persiste nce):
    > def __init__(self):
    > self.id = '' # How do I add a new incremental ID here?
    >>
    >># and later on in the app
    >>
    >>tkt = Ticket()
    >>dbroot[?????] = tkt
    >>
    >>How would one assign a unique ID to the root at that point?
    >
    Here's one way
    >
    class Sequence(Persis tence):
    def __init__(self):
    self.current = 0
    >
    def next(self):
    self.current += 1
    return self.current
    >
    ticketSequence = Sequence()
    >
    class Ticket(Persiste nce):
    def __init__(self):
    self.id = ticketSequence. next()
    Be aware that this isn't working concurrently. Depending on your
    application, this can be mitigated using a simple threading.Lock.

    Diez

    Comment

    • Thomas Guettler

      #3
      Re: Trying ZODB, background in Relational: mimic auto_increment?

      auto increment:
      Here's one way
      >
      class Sequence(Persis tence):
      def __init__(self):
      self.current = 0
      >
      def next(self):
      self.current += 1
      return self.current
      >
      ticketSequence = Sequence()
      >
      class Ticket(Persiste nce):
      def __init__(self):
      self.id = ticketSequence. next()
      >
      I am sorry, but this does not work if several threads or processes
      try to get a new id.

      Have a look at:

      "Resolving Conflicts".

      BTW, that's one of the points why I switched from ZODB to Postgres.

      HTH,
      Thomas

      --
      Thomas Guettler, http://www.thomas-guettler.de/
      E-Mail: guettli (*) thomas-guettler + de

      Comment

      Working...