peterknife / boto

Automatically exported from code.google.com/p/boto
0 stars 0 forks source link

Idea: naturally occurring id fields in boto.sdb.db #500

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Would be nice to define arbitrary Model fields as being object IDs.

For example, currently I have a User class where

     class User(Model):
            username = StringProperty(required=True, unique=True)

It makes sense for username to be the object id as well because

 -- it is unique for all objects of the class
 -- id fields are strings anyway
 -- username turns out to be shorter than the default ( generated
guids )
 -- username is more convinient to use in code

I can acheive username as object id manually in my code - but extra
steps like these are error-prone:
I can override the behavior ( and do ):

         u = User( id='alexander', username='alexander' )

   or

         u = User(username='alexander')
         u.id = u.username

Much nicer, and less error prone, would be defining the id field in
the class definition:

   class User(Model):
        ##unique=True is implicit, since username is also the id
        username = StringProperty(required=True, id=True )   

With that there are no extra steps to have id be username:

     u = User(username='alexa')
     u.save()

     u = User.find_by_id('alexa')

The method could even create composite keys, as in RDBs:

     Phone(Model):
          keysep="--"  # Use as separator when composing ID value
          model = StringProperty( id_field=True )
          mac_address = StringProperty( id_field=True )

What then happens is

     foo = Phone(model='iphone3', mac_address="11:22:33:44:55:66")

     foo.save()

Search benefit is syntactic sugar, but handling the return of

     obj = foo.get_by_id("iphone3--11:22:33:44:55:66" )

is simpler than for

     qobj = foo.find( model="iphone3", mac_address="11:22:33:44:55:66" ) 

Original issue reported on code.google.com by rmela02...@gmail.com on 25 Mar 2011 at 3:34