On Apr 20, 2010, at 7:06 PM, Rhett wrote:

> I've run into some difficulty getting the ORM to fit into an existing
> code base with some, I suppose, non-standard conventions.
> 
> One of the conventions is to not allow primary keys (auto-incremented
> integers) to be exposed on the front-end servlet or template
> but to maintain the original integer values inside 'logic' or 'back-
> end' code.
> 
> We've been through a few methods of trying this, but they feel like
> we're fighting sqlalchemy and must be missing something.


In Python, the closest thing we have to a "private" attribute is that it begins 
with an underscore.  This is extremely easy to do, just name the mapped 
attribute "_id" or whatever name you'd like.   This is documented at 
http://www.sqlalchemy.org/docs/mappers.html#customizing-column-properties .

> The plan is to flip a flag when the object passes through a pre-
> defined barrier, converting these columns only when they are asked
> for.
> 
> The original implementation had us creating two columns, one of them
> being a synonym which provides the encryption
> behavior, but this means we have class that look like this:
> class Foo(Base):
>  id, _id = build_id_column(Integer, primary_key=True)

where does "encryption" come into play ?     are you trying to just expose the 
"primary key" value as something else ?  easy enough, just use a descriptor:

class MyClass(object):
    def encrypted_id(self):
       return encrypt_my_id(self._super_secret_id_attribute)

    def set_encrypted_id(self, id):
        self._super_secret_id_attribute = unencrypt_id(id)

    encrypted_id = property(encrypted_id, set_encrypted_id)

mapper(MyClass, mytable, properties={
     '_super_secret_id_attribute':mytable.c.id
})

if you want MyClass.encrypted_id to be available in queries at the class level, 
this would require a SQL function that does your "encryption".  See 
examples/derived_attributes/ for some techniques on that.




-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to