Answer the question
In order to leave comments, you need to log in
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)
# ./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)
# ./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)
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
In [24]: classes
Out[24]: {'Message': messager.models.Message, 'User': users.models.User}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question