Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何找到sqlalchemy远程侧对象';s类或没有db查询的类名?_Python_Class_Sqlalchemy_Relationship_Relation - Fatal编程技术网

Python 如何找到sqlalchemy远程侧对象';s类或没有db查询的类名?

Python 如何找到sqlalchemy远程侧对象';s类或没有db查询的类名?,python,class,sqlalchemy,relationship,relation,Python,Class,Sqlalchemy,Relationship,Relation,让我们有一个类X和Y,以及它们之间的关系x2y和y2x。 从类映射器(class).iterate\u properties迭代器我们可以得到所有类的属性。 所以x2y和y2x是RelationshipProperty,我希望从中得到的是关系远端对象的类或类名 我已经试过解决问题了。 我找到了x2y.remote\u side[0].table.name,制作了一个tables\u映射,它将一个表名映射到一个类,对于一对多和一对一来说效果很好。如果我对多对多使用它,表名就是一个关联表 有关于如何

让我们有一个类X和Y,以及它们之间的关系x2y和y2x。 从类映射器(class).iterate\u properties迭代器我们可以得到所有类的属性。 所以x2y和y2x是RelationshipProperty,我希望从中得到的是关系远端对象的类或类名

我已经试过解决问题了。 我找到了
x2y.remote\u side[0].table.name
,制作了一个tables\u映射,它将一个表名映射到一个类,对于一对多和一对一来说效果很好。如果我对多对多使用它,表名就是一个关联表


有关于如何获取远程端类的提示吗?

X.x2y.property.mapper.class_

relatonshipproperty最终将获得与mapper现在相同的类级属性文档

编辑。下面是一个测试,它演示了上述从“X”返回“Y”,并且没有反射不会创建关系,因此应该没有影响:

from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
class X(Base):
    __tablename__ = 'x'
    id = Column(Integer, primary_key=True)
    x2y = relationship("Y")

class Y(Base):
    __tablename__ = 'y'
    id = Column(Integer, primary_key=True)
    x_id = Column(Integer, ForeignKey("x.id"))

assert X.x2y.property.mapper.class_ is Y

我发现relationshipproperty上的方法参数()返回远程类

for prop in class_mapper(X).iterate_properties:
    if isinstance(prop, RelationshipProperty):
        relation = prop
relation.argument()

这返回了X而不是Y。这是因为我使用了reflect吗?argument()只是最初传递给relationship()函数的参数,不一定是可调用的。“映射器”是关系所指的映射器。