pythonormponyorm

Reverse attribute not found in PonyORM


Function generate_mapping gives an error. I have models described separately in two modules.

I have a model:

# ./message/models.py
from pony import orm
from datetime import datetime
from db import db

model_names = ['Message', ]


class Message(db.Entity):
    id = orm.PrimaryKey(int, auto=True)
    time = orm.Required(datetime)
    text = orm.Required(str)
    user_from = orm.Required('User', cascade_delete=False)
    user_to = orm.Required('User', cascade_delete=True)

And another:

# ./user/models.py
from pony import orm
from datetime import datetime
from db import db

model_names = ['User', ]


class User(db.Entity):
    id = orm.PrimaryKey(int, auto=True)
    login = orm.Required(str, unique=True)
    pswdhash = orm.Required(str)
    last_login = orm.Required(datetime)
    is_active = orm.Required(bool, default=True)

The start of the program occurs via a common module db.py:

# ./db.py
from functools import reduce

from pony import orm

from settings import DATABASE, INSTALLED_APPS

db = orm.Database()
if DATABASE['engine'] == 'sqlite':
    db.bind(provider='sqlite', filename=DATABASE['path'], create_db=True)



pkgs = reduce(  # INSTALLED_APPS list
    lambda value, item: value + [__import__(f'{item}.models')],
    INSTALLED_APPS,
    [],
)
modules = reduce(  # pkg.models list
    lambda value, item: value + [getattr(item, 'models', [])],
    pkgs,
    [],
)
mod_names = reduce(  # models name in all pkg.models
    lambda value, item: value + getattr(item, 'model_names', []),
    modules,
    [],
)

classes = dict()  # dict with all models

for cn in mod_names:
    for m in modules:
        if cn in m.__dict__:
            classes[cn] = m.__dict__[cn]

print(classes)
db.generate_mapping(create_tables=True)

When I run the program in the terminal, I see this:

Traceback (most recent call last):
  File "D:\Programming\Python\cpython\v372\Lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "D:\Programming\Python\cpython\v372\Lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File ".\__main__.py", line 8, in <module>
    from db import db
  File ".\db.py", line 37, in <module>
    db.generate_mapping(create_tables=True)
  File "D:\Envs\hwpy2\lib\site-packages\pony\orm\core.py", line 967, in generate_mapping
    entity._link_reverse_attrs_()
  File "D:\Envs\hwpy2\lib\site-packages\pony\orm\core.py", line 3893, in _link_reverse_attrs_
    else: throw(ERDiagramError, 'Reverse attribute for %s not found' % attr)
  File "D:\Envs\hwpy2\lib\site-packages\pony\utils\utils.py", line 106, in throw
    raise exc
pony.orm.core.ERDiagramError: Reverse attribute for Message.user_from not found

Importing models is normal

In [24]: classes
Out[24]: {'Message': messager.models.Message, 'User': users.models.User}

What am I doing wrong?


Solution

  • In PonyORM you should set reverse attribute from both sides of the relation. In your case your User model should be

    class Message(db.Entity):
        id = orm.PrimaryKey(int, auto=True)
        time = orm.Required(datetime)
        text = orm.Required(str)
        user_from = orm.Required('User', cascade_delete=False, reverse='messages_sent')
        user_to = orm.Required('User', cascade_delete=True, reverse='messages_recieved')
    

    More detailes here

    class User(db.Entity):
        id = orm.PrimaryKey(int, auto=True)
        login = orm.Required(str, unique=True)
        pswdhash = orm.Required(str)
        last_login = orm.Required(datetime)
        is_active = orm.Required(bool, default=True)
        messages_sent = Set('Message', reverse='user_from')
        messages_recieved = Set('Message', reverse='user_to')
    

    Also since you have 2 links you should exactly specify which attribute to map.