ValueError: Attribute Users.request is required

ValueError: Attribute Users.request is required

我又需要帮助了。谁能告诉我为什么会出现此错误?我使用 Flask 和 pony orm 以及 python 3.4。我的文件很少。

users.py

from app import models
from pony.orm import *

class UsersAPI:
    @staticmethod
    @db_session
    def is_registered(e_mail):
        """
        This method for checking registered user or not
        :param e_mail:
        :return:
        """

        user = select(o for o in models.Users if o.email == e_mail)[:]
        print(user)

UsersAPI.is_registered("evgenusov96@yandex.ru")

文件models.py

from pony.orm import *
import config

db = Database()

class Users(db.Entity):
    id = PrimaryKey(int, auto=True)
    email = Required(str, 100)
    password = Required(str, 32)
    name = Required(str, 15)
    surname = Required(str, 20)
    city = Required(City, nullable=True)
    sex = Required(int)
    request = Required(str, 250, nullable=True)
    hobby = Required(str, 250, nullable=True)
    about = Required(str, 250, nullable=True)
    status_message = Required(str, 100, nullable=True)
    user1 = Set("Friends", reverse="user1")
    user2 = Set("Friends", reverse="user2")
    author = Set("Messages", reverse="author")
    addressee = Set("Messages", reverse="addressee")

我遇到的这个错误:

    C:\Python34\python.exe C:/Users/Евгений/PycharmProjects/langunet/run.py
Traceback (most recent call last):
  File "C:/Users/Евгений/PycharmProjects/langunet/run.py", line 2, in <module>
    from app import app
  File "C:\Users\Евгений\PycharmProjects\langunet\app\__init__.py", line 4, in <module>
    from app import routes
  File "C:\Users\Евгений\PycharmProjects\langunet\app\routes.py", line 2, in <module>
    from app.users import *
  File "C:\Users\Евгений\PycharmProjects\langunet\app\users.py", line 21, in <module>
    UsersAPI.is_registered("evgenusov96@yandex.ru")
  File "<string>", line 2, in is_registered
  File "C:\Python34\lib\site-packages\pony\orm\core.py", line 347, in new_func
    try: return func(*args, **kwargs)
  File "C:\Users\Евгений\PycharmProjects\langunet\app\users.py", line 18, in is_registered
    user = get(o for o in models.Users if o.email == e_mail)
  File "<string>", line 2, in get
  File "C:\Python34\lib\site-packages\pony\utils.py", line 88, in cut_traceback
    return func(*args, **kwargs)
  File "C:\Python34\lib\site-packages\pony\orm\core.py", line 4260, in get
    return make_query(args, frame_depth=3).get()
  File "<string>", line 2, in get
  File "C:\Python34\lib\site-packages\pony\utils.py", line 88, in cut_traceback
    return func(*args, **kwargs)
  File "C:\Python34\lib\site-packages\pony\orm\core.py", line 4531, in get
    objects = query[:2]
  File "<string>", line 2, in __getitem__
  File "C:\Python34\lib\site-packages\pony\utils.py", line 88, in cut_traceback
    return func(*args, **kwargs)
  File "C:\Python34\lib\site-packages\pony\orm\core.py", line 4721, in __getitem__
    return query._fetch(range=(start, stop))
  File "C:\Python34\lib\site-packages\pony\orm\core.py", line 4426, in _fetch
    used_attrs=translator.get_used_attrs())
  File "C:\Python34\lib\site-packages\pony\orm\core.py", line 3228, in _fetch_objects
    real_entity_subclass, pkval, avdict = entity._parse_row_(row, attr_offsets)
  File "C:\Python34\lib\site-packages\pony\orm\core.py", line 3258, in _parse_row_
    avdict[attr] = attr.parse_value(row, offsets)
  File "C:\Python34\lib\site-packages\pony\orm\core.py", line 1329, in parse_value
    val = attr.validate(row[offset], None, attr.entity, from_db=True)
  File "C:\Python34\lib\site-packages\pony\orm\core.py", line 1611, in validate
    if obj is None: throw(ValueError, 'Attribute %s is required' % attr)
  File "C:\Python34\lib\site-packages\pony\utils.py", line 126, in throw
    raise exc
ValueError: Attribute Users.request is required

如何解决?谢谢!

如果您将 User.request 属性定义为 Required,则可能会发生此错误,但在 table 中,相应的列实际上是可选的(即可以为空)。在加载对象期间,Pony 会检查对象的内部状态是否一致。由于缺少必需属性的值,Pony 生成异常。

我认为我们需要通过以下两种方式之一改进 Pony 的行为:

  • 即使需要一个属性,Pony 也应该允许从数据库中加载该属性的空值。
  • 另一种方法是使错误消息更清楚。