如何在没有数据库查询的情况下查找 sqlalchemy 远程端对象的类或类名?
让我们有一个类 X 和 Y 以及它们之间的关系 x2y 和 y2x。 从 class_mapper(Class).iterate_properties 迭代器中我们可以获得类的所有属性。 所以x2y和y2x是RelationshipProperty,我希望从中获得的是关系远程端对象的类或类名。
我已经尝试制定解决方案。 我找到了 x2y.remote_side[0].table.name,制作了一个tables_map,它将表名映射到类,并且它适用于一对多和一对一。如果我将它用于多对多,则表名称是关联表。
关于如何获得远程端类的任何提示?
Let's have a classes X and Y and relations between them x2y and y2x.
From class_mapper(Class).iterate_properties iterator we can get all class's properties.
So x2y and y2x are RelationshipProperty and what I hope to get from is a class or a class name of objects on remote side of relation.
I've already tried to make a solution.
I've found x2y.remote_side[0].table.name
, made a tables_map which maps a table name to a class and it works fine for one-to-many and one-to-one. If I use it for many-to-many the table name is an association table.
Any hints on how can I get the remote side class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
X.x2y.property.mapper.class_relatonshipproperty
最终将获得类级别的属性文档,就像mapper现在所做的一样。
编辑。这是一个测试,说明上面从“X”返回“Y”,并且没有反射不会创建关系,因此应该没有效果:
X.x2y.property.mapper.class_
relatonshipproperty will eventually get class-level attribute documentation the same as mapper does now.
edit. Here is a test which illustrates the above returning "Y" from "X", and no reflection doesn't create relationships so should have no effect:
我发现关系属性上的方法 argument() 返回远程类。
I've found that a method argument() on relationshipproperty returns remote class.