【发布时间】:2020-02-14 09:56:37
【问题描述】:
我最近开始使用 Pony ORM,我觉得它很棒。尽管官方网站上有详细的 api 文档,但我很难处理关系。特别是我想插入一个新实体,它是集合的一部分。但是,如果不先获取相关对象,我似乎无法找到创建实体的方法:
post = Post(
#author = author_id, # Python complains about author's type, which is int and should be User
author = User[author_id], # This works, but as I understand it actually fetches the user object
#author = User(id=author_id), # This will try and (fail to) create a new record for the User table when I commit, am I right?
# ...
)
表中最后只插入了id的值,为什么我只需要id就取整个对象呢?
编辑
我快速浏览了 Pony ORM 源代码,使用反向实体的主键应该可以工作,但即使在这种情况下,我们最终也会调用 _get_by_raw_pkval_ 从本地缓存或数据库中获取对象,因此可能是不可能的。
【问题讨论】:
标签: python mysql flask orm ponyorm