如何获取应该在关系的另一端的类的名称?它是在创建关系时宣布的。我想这些信息应该是sqlalchemy.orm.util.class_mapper的somwhere什么类型在sqlalchemy的关系的另一端没有创建对象?
假设我们有这三个类和它们之间的关系。 书* 1 ---大陆架和 书* --- *作者
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
从class_mapper(类).iterate_properties我们可以很容易地得到不同的性质:COLUMNPROPERTY和RelationshipProperty。 应该有一种方法可以获得相关类的名称。有任何想法吗?