On Mar 26, 2008, at 8:46 PM, alex bodnaru wrote:

>
>
> hello friends,
>
> is there a way to query a table object of all the relationships it's  
> involved with?
>
> both onoe2many, many2one, many2many etc.
>
> thanks in advance,
>

the raw info you can get from table.foreign_keys.  But those are  
collections of ForeignKey objects.   As far as if they are o2m, m2o,  
etc. , Tables don't know about that, nor does a Table have an  
immediate way to get foreign keys pointing to it.    You can write up  
something that puts all the FKs of a set of tables into a collection  
and works out the three directions based on the aggregate  
information.  If table A has an FK to table B, its m2o from A->B, and  
theres automatically o2m from B->A.  if table A has two FKs to tables  
B and C and no other columns, you can say theres a M2M between B and C  
across A.

if you already have mappers set up with relations, you can instead  
iterate through the properties on the mappers, something like:

from sqlalchemy.orm import sync
directions = {sync.ONETOMANY: 'onetomany', sync.MANYTOONE:'manytoone',  
sync.MANYTOMANY:'manytomany'}

for prop in class_mapper(SomeClass).iterate_properties:
     if hasattr(prop, 'direction'):
         print prop.key, directions[prop.direction]


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to