3

我正在尝试使用 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 的外键

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

4

1 回答 1

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)

这里

于 2019-03-02T05:57:40.187 回答