2017-07-17 17 views
1

Pony ORMで自分のテーブルを指定するときにこのエラーが発生します。pony.orm.core.ERDiagramError:不一致の逆属性

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 

マイペソアテーブルはCD_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") 

私は多くの文書と方法を試しましたが、それは成功しませんでした。

お時間をありがとうございます。

答えて

0

コードスニペットの問題は、間違って別のエンティティの主キーに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, 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属性を指定するには、必要ありません:だからPaisエンティティでpessoas属性のためにそれはPessoaエンティティでpais属性でなければなりません。 reverseは、エンティティ間の関係が1つ以上あり、自動リレーションシップ構築が不可能な場合にのみ指定する必要があります。あなたは、エンティティの関係についての詳細な情報を見つけることができ

ここ
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) 

を::

あなたが実体宣言からreverseを削除すると、それだけで正常に動作しますhttps://docs.ponyorm.com/relationships.html

また、あなたがオンラインを使用する場合がありますエンティティ - リレーションシップダイアグラムエディタhttps://editor.ponyorm.com/。アプリケーションのデータモデリングに役立ちます。

0

問題は、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 

PAISが実際にTB_PESSOAに存在しない、それがTA_PAISへの外部キーであるCD_PAISと呼ばれるフィールドです。