如果在保存时出现递归错误,如何在 PeeWee 中保存数据库对象

How to save a database object in PeeWee, if there's recursion error while saving

当我尝试保存一个对象的实例时,出现错误。
这是我的文件结构:

class Loader():
    dbhandle = SqliteDatabase('history.db')

    def __init__(self):
        try:
            Loader.dbhandle.connect()
            Hotel.create_table()
        except peewee.InternalError as px:
            print(str(px))
        try:
            HistoryObject.create_table()
        except peewee.InternalError as px:
            print(str(px))

    def save_to_database(self):
        history_object = HistoryObject(chat_id=0,
                                       command='123',
                                       date_time='123'
                                       )
        history_object.save()
    #...

class BaseModel(Model):
    class Meta:
        database = Loader.dbhandle

class HistoryObject(BaseModel):
    id = PrimaryKeyField(null=False)
    chat_id = IntegerField()
    command = CharField(max_length=100)
    date_time = CharField(max_length=100)

    class Meta:
        db_table = "historyObjects"

调用Loaderclass的save to database方法时,出现错误:

Traceback (most recent call last):
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 611, in infinity_polling
    self.polling(none_stop=True, timeout=timeout, long_polling_timeout=long_polling_timeout,
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 658, in polling
    self.__threaded_polling(non_stop, interval, timeout, long_polling_timeout, allowed_updates)
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 720, in __threaded_polling
    raise e
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 680, in __threaded_polling
    self.worker_pool.raise_exceptions()
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\util.py", line 135, in raise_exceptions
    raise self.exception_info
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\util.py", line 87, in run
    task(*args, **kwargs)
  File "C:\Users2\PycharmProjects\python_basic_diploma\Handlers\LowHighPrice.py", line 63, in handle
    make_query(message)
  File "C:\Users2\PycharmProjects\python_basic_diploma\Handlers\LowHighPrice.py", line 152, in make_query
    loader.save_to_database(0, '123', '123')
  File "C:\Users2\PycharmProjects\python_basic_diploma\loader.py", line 72, in save_to_database
    history_object.save()
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6577, in save
    pk_value = self._pk
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6542, in get_id
    return getattr(self, self._meta.primary_key.safe_name)
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4481, in __get__
    .where(self.field == getattr(instance, dest)))
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4481, in __get__
    .where(self.field == getattr(instance, dest)))
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4481, in __get__
    .where(self.field == getattr(instance, dest)))
  [Previous line repeated 488 more times]
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4479, in __get__
    return (self.rel_model
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6326, in select
    return ModelSelect(cls, fields, is_default=is_default)
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6994, in __init__
    super(ModelSelect, self).__init__([model], fields)
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6859, in __init__
    super(_ModelQueryHelper, self).__init__(*args, **kwargs)
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 2281, in __init__
    super(Select, self).__init__(**kwargs)
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 812, in __init__
    super(_HashableSource, self).__init__(*args, **kwargs)
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 768, in __init__
    super(Source, self).__init__()
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 2042, in __init__
    super(Query, self).__init__(**kwargs)
RecursionError: maximum recursion depth exceeded while calling a Python object
"
Traceback (most recent call last):
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 611, in infinity_polling
    self.polling(none_stop=True, timeout=timeout, long_polling_timeout=long_polling_timeout,
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 658, in polling
    self.__threaded_polling(non_stop, interval, timeout, long_polling_timeout, allowed_updates)
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 720, in __threaded_polling
    raise e
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 680, in __threaded_polling
    self.worker_pool.raise_exceptions()
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\util.py", line 135, in raise_exceptions
    raise self.exception_info
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\util.py", line 87, in run
    task(*args, **kwargs)
  File "C:\Users2\PycharmProjects\python_basic_diploma\Handlers\LowHighPrice.py", line 63, in handle
    make_query(message)
  File "C:\Users2\PycharmProjects\python_basic_diploma\Handlers\LowHighPrice.py", line 152, in make_query
    loader.save_to_database(0, '123', '123')
  File "C:\Users2\PycharmProjects\python_basic_diploma\loader.py", line 72, in save_to_database
    history_object.save()
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6577, in save
    pk_value = self._pk
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6542, in get_id
    return getattr(self, self._meta.primary_key.safe_name)
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4481, in __get__
    .where(self.field == getattr(instance, dest)))
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4481, in __get__
    .where(self.field == getattr(instance, dest)))
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4481, in __get__
    .where(self.field == getattr(instance, dest)))
  [Previous line repeated 488 more times]
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4479, in __get__
    return (self.rel_model
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6326, in select
    return ModelSelect(cls, fields, is_default=is_default)
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6994, in __init__
    super(ModelSelect, self).__init__([model], fields)
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6859, in __init__
    super(_ModelQueryHelper, self).__init__(*args, **kwargs)
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 2281, in __init__
    super(Select, self).__init__(**kwargs)
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 812, in __init__
    super(_HashableSource, self).__init__(*args, **kwargs)
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 768, in __init__
    super(Source, self).__init__()
  File "C:\Users2\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 2042, in __init__
    super(Query, self).__init__(**kwargs)
RecursionError: maximum recursion depth exceeded while calling a Python object

Loader class 也用于使用 pyTelegramBotApi 和从另一个文件调用的方法实例化和轮询机器人。不知道会不会有问题
class Loader 本身总是实例化一次,所以 __init__ 方法也必须只调用一次
更多信息:在尝试调试它时,我在 peewee.py 包文件 getattr 中写了一个打印函数来打印 dest 变量。它总是等于 'id'.

问题出在数据库的另一个 table 中,他与第一个 ForeignKeyField(HistoryObject, related_name='id') 相关。属性 related_name 破坏了所有内容,删除属性后一切正常