That was exactly what I was looking for. Thanks a lot :-)

Simon

On 21 Aug., 19:57, Michael Bayer <mike...@zzzcomputing.com> wrote:
> On Aug 21, 2010, at 12:05 PM, simon wrote:
>
> > Hi all,
>
> > For a given declaratively mapped class in SA 0.6, how do I
> > programmatically find out all other mapped classes forming a many-to-
> > one or many-to-many relationship with it? I know I can ask a table
> > object for all its foreign keys, but the information whether it forms
> > a many-to-one or a many-to-many relationship is not really contained
> > in the schema.
>
> Assuming that relationship() has been configured, you'd retrieve the Mapper 
> for each class:
>
>         from sqlalchemy.orm import class_mapper
>
>         mapper = class_mapper(someclass)
>
> you can then iterate through its properties looking for what you need, but 
> note the API of MapperProperty/RelationshipProperty is less stable than that 
> of primary ORM constructs:
>
>         from sqlalchemy.orm.properties import RelationshipProperty
>         from sqlalchemy.orm.interfaces import MANYTOMANY, MANYTOONE
>
>         for prop in mapper.iterate_properties:
>                 if isinstance(prop, RelationshipProperty):
>                         prop.direction in (MANYTOMANY, MANYTOONE) ...
>
> if OTOH your asking for relationship() to be generated automatically, there's 
> no completely reliable way to tell if an FK is many to one or many to many, 
> unless you want to make a guess based on the other columns and constrants on 
> the referencing table.
>
>
>
> > Thanks, Simon
>
> > --
> > 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 
> > athttp://groups.google.com/group/sqlalchemy?hl=en.

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