0

Very simple senario:

class User(Persistent):
  def __init__(self, username, email):
    self.username = username
    self.email = email

I want to store the User instances in zodb by its username while keeping email unique.

u1 = User(username="u1",email="u1@example.com")
zodb_container[u1.username] = u1

I use the username as key to store this object, so it will be unique. My question is how to keep the email attribute unique? Is there any elegant way to do this in an object database?

Xiong
  • 577
  • 1
  • 7
  • 12

1 Answers1

2

No.

ZODB is somewhat misnamed. It's not a "database" like Postgres or MySQL is a database that has constraints and a query language and all that stuff that people presume a database has. It's, instead, a persistent object store. There's nothing that ZODB does for you that Python's pickle module doesn't do for you except manage working sets that are larger than available memory, handle transactions, and allow you to share object representations between processes. It's in effect "superpickle".

So your answer wouldn't be any different than if you had asked the same question about pickle, really.

Chris McDonough
  • 2,471
  • 17
  • 17
  • Hi Chris, thank you very much! I wonder when you meet such a requirement, how you solve it and what's the recommended practice? I have two options right now: 1, combine ZODB with a real relational database. 2, create another dictionary in ZODB and use the email address as the key. So I maintain two dictionary to keep both username and email address unique. – Xiong Jan 03 '13 at 21:40
  • Suppose I choose the 2nd option, I have another question: If I save the same User object twice in two dictionary, will ZODB keep two copies of that User object or just one copy? If ZODB keeps two copies, then I have to just save a key(User.username) instead of the whole User object. BTW, I think I'd better do an experiment right away. – Xiong Jan 03 '13 at 21:50
  • ZODB only keeps one copy of the User object. So I am going to use two dictionary to keep both attribute unique. – Xiong Jan 03 '13 at 22:18