castiel castiel - 1 year ago 99
Python Question

how to get backref name in sqlalchemy?

class User(Base):
__tablename__ = 'users'

addresses = relationship('Address', order_by='', backref='user')

class Address(Base):
__tablename__ = 'addresses'

user_id = Column(Integer, ForeignKey(''))

The above code shows a very common one-to-many configuration in SQLAlchemy.

How can I get the
name which is 'user' in the
class from a instance of
? How can I know that I have a attribute named 'addresses' in the
class which indicates that
has many addresses?

I can get all the column information from the
attribute, but seems like these are stored there as well.

Answer Source

we're trying to encourage use of the inspect() api new in 0.8:

>>> from sqlalchemy import inspect
>>> inspect(User).attrs.keys()
['addresses', 'id']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download