函数generate_mapping给出错误。我在两个模块中分别描述了模型。
我有一个模特:
# ./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
进行:
# ./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)
当我在终端中运行程序时,会看到以下内容:
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}
我在做什么错了?
答案 0 :(得分:1)
在PonyORM中,您应该从关系的两侧设置反向属性。
您的User
模型应该是
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')
更多详细信息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')
此外,由于您有2个链接,因此您应准确指定要映射的属性。