How to obtain a name of the class that should be on the other end of the relation? It was declared when creating relationship. I guess that information should be somwhere in sqlalchemy.orm.util.class_mapper
Let's say we have these three classes and a relations between them. Book * --- 1 Shelf and Book * --- * Author
class Shelf(Base):
__tablename__ = 'shelves'
id = Column(Integer, primary_key = True)
name = Column(String)
#one-to-many books by backref in Book.shelf
def __init__(self, name=""):
self.name = name
class Book(Base):
__tablename__ = 'books'
id = Column(Integer, primary_key = True)
title = Column(String)
#many-to-one shelf
shelf_id = Column(Integer, ForeignKey('shelves.id'))
shelf = relationship(Shelf, backref=backref('books', order_by=id))
def __init__(self, title=""):
self.title = title
author_book = Table('author_book', metadata,
Column('author_id', Integer, ForeignKey('authors.id')),
Column('book_id', Integer, ForeignKey('books.id'))
)
class Author(Base):
__tablename__ = 'authors'
id = Column(Integer, primary_key = True)
name = Column(String)
#many-to-many books
books = relationship('Book', secondary=author_book, backref='authors')
def __init__(self, name=""):
self.name = name
From class_mapper(Class).iterate_properties we can easily get different properties: ColumnProperty and RelationshipProperty. There should be a way to get at leat the name of the class that is in relation. Any ideas?