[sqlalchemy] Re: returning primary key of object without know what it is called.

2008-12-05 Thread King Simon-NFHD78

 -Original Message-
 From: sqlalchemy@googlegroups.com 
 [mailto:[EMAIL PROTECTED] On Behalf Of Faheem Mitha
 Sent: 04 December 2008 20:43
 To: sqlalchemy@googlegroups.com
 Subject: [sqlalchemy] returning primary key of object without 
 know what it is called.
 
 
 
 Hi,
 
 I'm trying to figure out how to have an object return its primary key 
 without knowing what it is called. The docs in 
 http://www.sqlalchemy.org/docs/05/sqlalchemy_orm_mapper.html look 
 relevant, for example the function identity_key_from_instance 
 (see entry 
 from docs below), but I'm not clear about usage. The 
 functions on this 
 page look like they are meant to be used as method functions 
 of a mapper 
 object, but how should I construct such a mapper object? In my schema 
 file, I have lines like
 
 Mapper(Foo, foo_table)
 
 should I be returning an mapper object for use with 
 functions? Ie should I 
 be doing
 
 foo_mapper = Mapper(Foo, foo_table)
 
 or similar? The section module sqlalchemy.orm.mapper saya
 
 This is a semi-private module; the main configurational API 
 of the ORM is 
 available in module sqlalchemy.orm.
 
 Does this mean it is not meant to be used in this fashion?
 
 Also, I don't understand what is meant by
 
 This value is typically also found on the instance state under the
 attribute name key.
 
 in the docs for identity_key_from_instance below.
 
 Please CC me on any reply.
Thanks and regards, Faheem.
 
 

You can get the mapper for a given instance using the
sqlalchemy.orm.object_mapper function, and that mapper has a
'primary_key_from_instance' method. A generic primary_key function might
look like this (untested):


import sqlalchemy.orm as orm

def get_primary_key(instance):
   mapper = orm.object_mapper(instance)
   return mapper.primary_key_from_instance(instance)

Hope that helps,

Simon

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



[sqlalchemy] Re: returning primary key of object without know what it is called.

2008-12-05 Thread Faheem Mitha



On Fri, 5 Dec 2008, King Simon-NFHD78 wrote:

 You can get the mapper for a given instance using the
 sqlalchemy.orm.object_mapper function, and that mapper has a
 'primary_key_from_instance' method. A generic primary_key function might
 look like this (untested):

 import sqlalchemy.orm as orm

 def get_primary_key(instance):
   mapper = orm.object_mapper(instance)
   return mapper.primary_key_from_instance(instance)

 Hope that helps,

Hi Simon,

Thanks, that is very helpful. That's exactly what I need.

  Regards, Faheem.

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