On Aug 15, 2012, at 11:35 AM, Jason wrote:

> Is there a way to determine if a relationship property on a model class is a 
> backref?

not in the strict sense, because a "backref" is just configurational sugar for 
two separate relationship() objects that are aware of each other.    But you 
can determine one of those relationship() objects given the other one.

> 
> For instance I have the model:
> 
> class Department(Base):
>     department_id = Column(Int(), primary_key=True)
> 
> class Employee(Base):
>     employee_id = Column(Int(), primary_key=True)
>     department_id = Column(Int(), ForeignKey(Department.deptartment_id), 
> nullable=False)
>     department = relationship(Department, backref='employees')
> 
> I'm looping through the attributes of each table and for each attribute that 
> has a relationship property I want to determine whether the relationship 
> constructor was called on the current table or not. So for 
> Department.employees it would be False because the relationship was defined 
> as a backref from another table. Is there a method/technique to accomplish 
> this?

in this case it seems like you aren't as much concerned about the actual mapper 
configuration as you are about codepaths being invoked.   So we "instrument" 
functions to track when they are called.

You'd make a wrapper around relationship() yourself, which would be used by 
userland code.  This allows userland relationship() calls to be distinguished 
from those that SQLAlchemy calls itself internally:

from sqlalchemy.orm import relationship as _relationship
relationships_called = set()
def relationship(*arg, **kw):
    rel = _relationship(*arg, **kw)
    relationships_called.add(rel)
    return rel

def was_relationship_called(rel):
    return rel in relationships_called


-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to