Я сохраняю некоторые данные телеграммы в базе данных, но когда я пытаюсь использовать SESSION.add(cursor)
, я получаю эту ошибку:
sqlalchemy.orm.exc.UnmappedInstanceError: Class 'builtins.NoneType' is not mapped
Это мой файл db init :
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
from tg_userbot import DB_URI
def start() -> scoped_session:
engine = create_engine(DB_URI, client_encoding="utf8")
BASE.metadata.bind = engine
BASE.metadata.create_all(engine)
return scoped_session(sessionmaker(bind=engine, autoflush=False))
BASE = declarative_base()
SESSION = start()
Вот мой класс таблицы:
class STATS(BASE):
__tablename__ = "stats"
totaldialogs = Column(Integer, primary_key=True)
usercount = Column(Integer)
channelcount = Column(Integer)
supcount = Column(Integer)
convertedgroups = Column(Integer)
numchannel = Column(Integer)
numuser = Column(Integer)
numchat = Column(Integer)
numsuper = Column(Integer)
def __init__(
self, totaldialogs, usercount, channelcount, supcount,
convertedgroups, numchannel, numuser, numchat, numsuper,
):
self.totaldialogs = totaldialogs
self.usercount = usercount
self.channelcount = channelcount
self.supcount = supcount
self.convertedgroups = convertedgroups
self.numchannel = numchannel
self.numuser = numuser
self.numchat = numchat
self.numsuper = numsuper
А вот я пытаюсь добавить некоторые значения:
db = SESSION.query(STATS).first()
if not db:
STATS(0, 0, 0, 0, 0, 0, 0, 0, 0)
SESSION.add(db)
SESSION.commit()
Теперь, когда я вручную вставляю некоторые значенияс помощью инструмента sql GUI значения добавляются просто отлично.Но если таблица пуста, появляется ошибка.