我在使用 SQLAlchemy 经典映射时有点挣扎。我的情况如下:
class Manager(object):
def __init__(self):
self.id = uuid.uuid4()
.....
class Client(object):
def __init__(self):
self.id = uuid.uuid4()
self.manager = None
我想做的是说服 SQLAlchemy 在客户端和管理器(客户端有管理器)之间创建关系,而不必在客户端级别定义管理器 id 属性。在上面的代码中,'manager' 属性将是 Manager 类的一个实例。在这种情况下,我使用的是经典映射。任何帮助将非常感激。
mapper(Client, self._table, column_prefix='_', properties={'managed_by_relation':relationship(User, order_by=self._table.c.id, foreign_keys=[self._table.c.manager_id]))
上面的代码描述了我试图避免的事情。