【问题标题】:Python PonyORM One to one mappingPython PonyORM 一对一映射
【发布时间】:2019-02-10 12:35:40
【问题描述】:

我正在尝试使用 Pony ORM 创建一对一映射。

class ApplierIngress(ApplierObjectMapper.db.Entity):
    correlation_id = orm.PrimaryKey(str)
    ticket_number  = orm.Required(str)
    username       = orm.Required(str)
    status         = orm.Required(str)
    request_date   = orm.Required(datetime)

class ApplierResult(ApplierObjectMapper.db.Entity):
    correlation_id = orm.Required(ApplierIngress)
    result         = orm.Required(orm.LongStr)
    request_date   = orm.Required(datetime)

生成映射时抛出错误

pony.orm.core.ERDiagramError: Reverse attribute for ApplierResult.correlation_id not found

我希望 ApplierResult 表中的 correlation_id 是引用 ApplierIngress 表中的 correlation_id 的外键

请让我知道我做错了什么?

【问题讨论】:

    标签: python-3.x ponyorm


    【解决方案1】:

    正如错误所说,您需要指定反向属性。实体不仅仅是表格。

    class ApplierIngress(ApplierObjectMapper.db.Entity):
        correlation_id = orm.PrimaryKey(str)
        ticket_number  = orm.Required(str)
        username       = orm.Required(str)
        status         = orm.Required(str)
        request_date   = orm.Required(datetime)
        result_id      = orm.Optional('ApplierResult') # this attribute should be added       
    
    class ApplierResult(ApplierObjectMapper.db.Entity):
        correlation_id = orm.Required(ApplierIngress)
        result         = orm.Required(orm.LongStr)
        request_date   = orm.Required(datetime)
    

    由于这个类还没有被声明,你应该把它用作字符串或 lambda

    result_id = orm.Optional('ApplierResult')

    result_id = orm.Optional(lambda: ApplierResult)

    here

    【讨论】:

    • 如果我不需要反向怎么办?例如:如果我有一个只能通过其元素 ID/主键查询的表?
    • @meyer1994 你了解表和实体的区别吗?如果您出于某种原因想要将实体作为表格使用 - 只需使用 result_id = orm.Optional(int)。但这不是我们期望的行为。
    猜你喜欢
    • 2022-08-10
    • 1970-01-01
    • 2011-06-14
    • 2021-08-18
    • 2012-01-21
    • 2018-05-13
    • 2012-04-21
    • 1970-01-01
    相关资源
    最近更新 更多