C
C
CaufMAN2019-06-10 06:00:54
Python
CaufMAN, 2019-06-10 06:00:54

How to fix generate_mapping error when splitting model definition in PonyORM?

I have two modules with model definitions:

# ./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 one more
# ./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 work is started through the 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
    lambda value, item: value + [__import__(f'{item}.models')],
    INSTALLED_APPS,
    [],
)
modules = reduce(  # получение списка модулей models из списка модулей приложений
    lambda value, item: value + [getattr(item, 'models', [])],
    pkgs,
    [],
)
mod_names = reduce(  # получение списка имен классов моделей
    lambda value, item: value + getattr(item, 'model_names', []),
    modules,
    [],
)

classes = dict()  # словарь с классами моделей

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 application, I catch the following bug:
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

As far as I understand, the orm cannot pull up a connection from User, since objects in the database have not yet been created for it. When imported, both models are defined correctly:
In [24]: classes
Out[24]: {'Message': messager.models.Message, 'User': users.models.User}

So how to define a link to another model from another module?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
NikVlasov, 2017-07-16
@NikVlasov

Need to do it on bootstrap?
or is it not so important?

B
Barmunk, 2017-07-16
@Barmunk

like this? getbootstrap.com/examples/dashboard

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question