On Feb 13, 2014, at 6:23 AM, Guido Winkelmann <gu...@ambient-entertainment.de> 
wrote:

> Hi,
> 
> Reading through the docs, I could not find out how to tell the cardinality of 
> an object of type sqlalchemy.orm.properties.RelationshipProperty, i.e. 
> whether it is one-to-many, many-to-one or many-to-many.  Mostly, I just need 
> to know whether a relationship refers to just one object or a collection of 
> objects.
> 
> Can someone help me with that?

So given a class:

MyClass

and a relationship at the class level:

MyClass.some_relationship

you get at the RelationshipProperty like this:

MyClass.some_relationship.property

it then has:

1. the “uselist” flag, which is likely the most direct way for you to see 
“collection” or not:

    MyClass.some_relationship.property.uselist

2. the “direction”, which is MANYTOMANY, ONETOMANY, MANYTOONE:

  from sqlalchemy.orm import interfaces

  MyClass.some_relationship.property.direction is interfaces.MANYTOONE
  MyClass.some_relationship.property.direction is interfaces.ONETOMANY
  MyClass.some_relationship.property.direction is interfaces.MANYTOMANY


these flags will be available as of when the “configure_mappers()” step has 
been called, which is usually automatic.    If you are calling these attributes 
immediately after declaring the classes, they might not yet be available in 
which case the configure_mappers() call will force that to happen.

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to