【问题标题】:pony.orm.core.ERDiagramError: Inconsistent reverse attributepony.orm.core.ERDiagramError:反向属性不一致
【发布时间】:2017-07-17 19:57:32
【问题描述】:

在 Pony ORM 上指定我的表时出现此错误。

  File "business.py", line 79, in <module>
    db.generate_mapping()
  File "<string>", line 2, in generate_mapping
  File "/home/ancinedev/.pyenv/versions/3.6.1/lib/python3.6/site-packages/pony/utils/utils.py", line 58, in cut_traceback
    return func(*args, **kwargs)
  File "/home/ancinedev/.pyenv/versions/3.6.1/lib/python3.6/site-packages/pony/orm/core.py", line 724, in generate_mapping
    entity._link_reverse_attrs_()
  File "/home/ancinedev/.pyenv/versions/3.6.1/lib/python3.6/site-packages/pony/orm/core.py", line 3511, in _link_reverse_attrs_
    throw(ERDiagramError, 'Inconsistent reverse attributes %s and %s' % (attr, attr2))
  File "/home/ancinedev/.pyenv/versions/3.6.1/lib/python3.6/site-packages/pony/utils/utils.py", line 96, in throw
    raise exc
pony.orm.core.ERDiagramError: Inconsistent reverse attributes Pais.pessoas and Pessoa.identificador

我的 Pessoa 表有一个名为 CD_PAIS 的属性,该属性是对 Pais 表的引用,在该表中它被设置为主键。

class Pais(db.Entity):
    _table_ = ['SAD', 'TA_PAIS']
    codigo = PrimaryKey(int, column="CD_PAIS")
    nome = Required(str, column="NM_PAIS")
    pessoas = Set(lambda: Pessoa, reverse="identificador")

class Pessoa(db.Entity):
    _table_ = ['SAD', 'TB_PESSOA']
    identificador = PrimaryKey(int, column="ID_PESSOA")
    nome = Required(str, column="NM_PESSOA")
    tipo_pessoa = Required(str, column="IN_TIPO_PESSOA")
    numero_registro = Optional(str, column="NR_REGISTRO")
    pais = Required(Pais, reverse="codigo")

我尝试了许多文档和方法,但都没有成功。

感谢大家的宝贵时间。

【问题讨论】:

    标签: python orm pony ponyorm


    【解决方案1】:

    您的代码 sn-p 的问题是您错误地将 reverse 属性指向另一个实体的主键。首先,反向属性应该是另一个实体的关系属性,而不是它的主键。所以对于Pais实体中的pessoas属性,它应该是Pessoa实体中的pais属性:

    class Pais(db.Entity):
        _table_ = ['SAD', 'TA_PAIS']
        codigo = PrimaryKey(int, column="CD_PAIS")
        nome = Required(str, column="NM_PAIS")
        pessoas = Set(lambda: Pessoa, reverse="pais")
    
    class Pessoa(db.Entity):
        _table_ = ['SAD', 'TB_PESSOA']
        identificador = PrimaryKey(int, column="ID_PESSOA")
        nome = Required(str, column="NM_PESSOA")
        tipo_pessoa = Required(str, column="IN_TIPO_PESSOA")
        numero_registro = Optional(str, column="NR_REGISTRO")
        pais = Required(Pais, reverse="pessoas")
    

    但是在这个例子中指定reverse 属性是没有必要的,因为Pony 可以自己找出关系属性。 reverse 仅在实体之间存在多个关系并且无法自动建立关系时才需要指定。

    如果您从实体声明中删除 reverse,它将正常工作:

    class Pais(db.Entity):
        _table_ = ['SAD', 'TA_PAIS']
        codigo = PrimaryKey(int, column="CD_PAIS")
        nome = Required(str, column="NM_PAIS")
        pessoas = Set(lambda: Pessoa)
    
    class Pessoa(db.Entity):
        _table_ = ['SAD', 'TB_PESSOA']
        identificador = PrimaryKey(int, column="ID_PESSOA")
        nome = Required(str, column="NM_PESSOA")
        tipo_pessoa = Required(str, column="IN_TIPO_PESSOA")
        numero_registro = Optional(str, column="NR_REGISTRO")
        pais = Required(Pais)
    

    您可以在此处找到有关实体关系的更多信息:https://docs.ponyorm.com/relationships.html

    您可能还想使用在线实体关系图编辑器https://editor.ponyorm.com/。它可以帮助您为您的应用程序进行数据建模。

    【讨论】:

      【解决方案2】:

      问题出在生成SQL的时候。

      File "/home/dev/.pyenv/versions/3.6.1/lib/python3.6/site-packages/pony/orm/dbapiprovider.py", line 55, in wrap_dbapi_exceptions
          except dbapi_module.DatabaseError as e: raise DatabaseError(e)
      pony.orm.dbapiprovider.DatabaseError: ORA-00904: "TB_PESSOA"."PAIS": invalid identifier
      
      SELECT "TB_PESSOA"."ID_PESSOA", "TB_PESSOA"."NM_PESSOA", "TB_PESSOA"."IN_TIPO_PESSOA", "TB_PESSOA"."NR_REGISTRO", "TB_PESSOA"."PAIS"
      FROM "SAD"."TB_PESSOA" "TB_PESSOA"
      WHERE 0 = 1
      

      实际上TB_PESSOA中不存在PAIS,它是一个叫CD_PAIS的字段,是TA_PAIS的外键。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-22
        • 1970-01-01
        • 1970-01-01
        • 2017-06-14
        • 1970-01-01
        • 2014-08-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多