I am getting this error when specifying my tables on 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
My Pessoa table has an attribute called CD_PAIS and this attribute is a reference to Pais table, where it is set as a Primary Key.
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")
I tried many documentations and ways but was not success on that.
Thanks everyone for your time.
The problem with your code snippet is that you mistakenly point the
reverse
attribute to the primary key of another entity. First of all, the reverse attribute should be the relationship attribute of another entity, not its primary key. So for thepessoas
attribute inPais
entity it should be thepais
attribute in thePessoa
entity:But in this example specifying the
reverse
attribute is not necessary, because Pony can figure out the relationship attributes itself. Thereverse
need to be specified only if you have more that one relationship between entities and automatic relationship building is not possible.If you remove the
reverse
from the entities declarations, it will work just fine:Here you can find more information about entity relationships: https://docs.ponyorm.com/relationships.html
Also you might want to use the online Entity-Relationship Diagram Editor https://editor.ponyorm.com/. It can help you with data modeling for your application.